Web 601 – Regular Expressions and JavaScript Validation

JavaScript can be used to validate a form before it is submitted. To do this we can set the onsubmit value of a form to that of a JavaScript function.

js.PNG

In this case we are checking the whole form when the submit button is clicked.

js.PNG

You can also use the “onblur” function to check a forms input field once it loses focus. By losing focus it means when the field is no longer selected. We can therefore check the name validation when the user moves off of that input field.

js

The above image also shows the checking of matching passwords. Which uses the following code.

js.PNG

However, the regular expression aspect of this is found within the checkName function. This regular expressions requires the input to contain 1 alphabetic character followed by 7 characters.

js

The regular expression above is: /^[a-zA-Z].{7}$/

Advertisements
Web 601 – Regular Expressions and JavaScript Validation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s