This Java Script demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address.I have borrowed the regex for verifying the correct email format from a different site. Please note that I have removed the action attribute because this is only a demo and we are only concerned with the client-side verification of the Java Script function.This function either returns nothing when everything is ok or an object stating what went wrong.A very common use case of a custom validator is to check wether the form matches the sanitization rules of the server, this means the validator checks if the characters your user puts into your form are allowed. If the strings are different, an alert warns the user that the email addresses are not the same and returns a false value.If we have passed the two tests, then else block in lines 11-14 gets executed.
When you submit a form to a CGI program that resides on the server, it is usually programmed to do its own check for errors.This is the best book on the subject I’ve ever read, showing you step-by-step the smartest ways to create usable, useful DHTML effects to enhance your site. Site Point ships anywhere in the world and you can download the first 4 chapters for free. This method accepts regex expressions as arguments to check the string and returns the position of the specified value in the string or a -1 if no match was found.If the search() method returns a -1, then an alert would tell the user that an invalid email address has been entered and returns a false value.