jeudi 16 avril 2015

How is this Phone Number Validation work?

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!


Aucun commentaire:

Enregistrer un commentaire