-5

so I'm teaching myself jQuery recently and I just came to the chapter of AJAX. There is an example showing how to check the validation for phone number and I don't know what's going on and how it works. Here is the snippet of code:

// Validate a phone number field
$( "#form" ).submit(function( event ) {
    var inputtedPhoneNumber = $( "#phone" ).val();

    // Match only numbers
    var phoneNumberRegex = /^\d*$/;

    // If the phone number doesn't match the regex
    if ( !phoneNumberRegex.test( inputtedPhoneNumber ) ) {

        // Usually show some kind of error message here

        // Prevent the form from submitting
        event.preventDefault();
    } else {

        // Run $.ajax() here
    }
});

What I don't understand is this:

var phoneNumberRegex = /^\d*$/;

I did some research for phone number validation and it all has something like this: a pair of "/ /" and some stuff between them and I've never seen this before, anyone can explain what is going on here and why it works? Any help will be appreciated, thank you!

Jonathan
  • 7,345
  • 3
  • 31
  • 63
OD Street
  • 877
  • 2
  • 11
  • 19
  • you may want to play with : http://lea.verou.me/regexplained/ – nobe4 Apr 16 '15 at 20:46
  • it is just checking to see that it is all numeric digits. Nothing else. That what the "d" is shorthand for. – dgig Apr 16 '15 at 20:48
  • \d is similar to[0-9] and ^ means begin with $ means end with.In this case digits. * means zero or more occurences – AAB Apr 16 '15 at 20:48
  • [Your regex's explanation](https://regex101.com/r/jE0nC5/1) – kei Apr 16 '15 at 20:49

3 Answers3

2

In JavaScript a regular expression can be defined by placing a regular expression pattern between two / slash characters.

In the case of your example the regular expression pattern means:

  • ^ - start matching from the very start of the string sequence;
  • \d* - match zero or more digit characters 0-9;
  • $ - keep matching until the very end of the string sequence.

In short, this pattern requires that the entire string sequence is composed of only digit characters and nothing else.

The resulting regular expression object can then call the test function on a string sequence to test whether or not that sequence fits the pattern, and it will only evaluate to true if the string sequence exactly matches the regular expression.

Bobulous
  • 12,241
  • 4
  • 36
  • 63
0

/^\d*$/ simply matches a string that begins with (^) a number, contains an arbitrary amount of digits (\d*) and also ends with a number ($).

Etheryte
  • 20,940
  • 10
  • 58
  • 98
0

http://regexr.com/3ar56 - This will give you an idea of what the regex statement does.

var phoneNumberRegex = /^\d*$/;

the above code sets the variable phoneNumberRegex with a regex statement. The statement does nothing on it's own, but

!phoneNumberRegex.test( inputtedPhoneNumber )

tells it to test the variable inputtedPhoneNumber to see if it is not formatted the way the regex statement suggests it should be. ^\d sees if it starts with a digit * says that it will accept any number of the previous regex property, in this case \d, which tests for a digit. $ states that when the last digit is given it should stop matching. If you put a space after the number it won't match.

so this: ^/d*$

says: if it starts with a digit collect everything between that first digit and the end of the digit string.

If it the string inside inputtedPhoneNumber doesn't have the specified format it won't match.

zfrisch
  • 7,744
  • 1
  • 18
  • 31