Validating input in java script


07-Oct-2017 07:00

Incase you didn't provide information in the format specified by the form field or leave it empty, the message will appear and form cannot be submitted until you get it right.

This is done using a Javascript program on the client side, the Javascript program uses a regular expression pattern to test the input of each form field.

Next, we use regular expression and the test() method to check the field for a compliance.

Also we will use trim() function that will trim leading whitespace off the string.

validate Email ( ) Next we want to see if the email address the user entered is real.

This means that the input data must contain at least an @ sign and a dot (.).

The regular expression is used to find the characters and then replace them with empty spaces.

Unwanted parentheses surrounding area codes or telephone numbers, can be removed using regular expression and method replace() The regular expression searches for one or more parentheses, spaces or dashes , globally.

validating input in java script-51

free business dating sites

validate Password ( ) The function below checks the password field for blankness and allow only letters and numbers - no underscopes this time.

Usually, in a form some fields are optional while some fields are mandatory, hence a javascript program is used to check if the input field is empty or null.

The demo below , uses a function to check if value in the form field is empty or null.

If its not blank, the function returns an empty string.

validate Username ( ) The function below checks if the user entered anything at all in the username field.

This wont be perfect validation it is possible to slip not compliant addresses by it but it's normally good enough.