Validating phone number
In most browsers - those that support Java Script 1.5 (Firefox, Chrome, Safari, Opera 7 and Internet Explorer 8 and higher) - you can use more powerful regular expressions. The code presented above is fine in that it checks everything that we wanted to check, but uses a lot of code to test each requirement individually and present different error messages.We're going to show you now how to apply the password tests using a single regular expression.If the purpose of registration is to confirm that the person exists, and that they have supplied a valid email address, then as part of the registration processe you a should either email them a random password or a confirmation token rather than letting them choose their own password and use it immediately.The code presented below would then be used for letting the user change their password.
At this stage both Firefox and Opera enforce HTML5 validation attributes in the browser while Safari only lets you use them with Java Script or for CSS effects.
Again, you can use the form below to test this regular expression: Restricting which characters can be used is not good practice as punctuation and other symbols provide extra security.
You might implement this code on your own website as follows: As you can see, it's well worth learning the intricacies of regular expressions.
Remember that, as Java Script isn't available in all browsers, you should also use server-side scripting to validate all data before recording it in a database or elsewhere.
You might also want to spice up your forms using HTML5 Form Validation as we've done further down the page.