Home » Web Development » 05 - Client Side Scripting Concepts
5

Login, Password and more

How to program javascript involving login, passwords, etc

Login:

validField = /^[A-Za-z][\w\-]{5,}$/;

The above line of code is valid syntax for declaring a Javascript regular expression. The expression is specified between two back-slashes. This creates a regular expression initialized to the pattern within the back slashes. In the above expression, putting the entire string between a ^ and a $ implies that we want the entire string being searched - from beginning to end - to match the specified pattern. '[A-Za-z]' looks for a single alphabet at the start of the pattern. '[\w\-]' matches any digit, alphabet, _ and also a literal hyphen. We say that we want at least five of these in our pattern using {5,}. The reason we have not specified an upper bound is because we have restricted the size of the login form field to 10 characters in the HTML.

Password:

validField = /^[\w\-]{6,9}$/;

Here we simply allow the user to enter any alphabet or digit and _ and - in addition. We restrict the size to 6-9 character using '{6,9}'

First Name and Last Name:

validField = /^[A-Za-z][A-Za-z\-\_'` ]{0,19}$/;

We stipulate an initial alphabet using ^[A-Za-z]. Afterwards, we accept upto nineteen alphabets, hyphens, underscores, single quotes, or spaces. Names sometimes contain hyphens and quotes.

Phone Number

validField = /^[0-9\+][0-9]{4,20}$/;

The phone number may contain a digit or a '+' in the first field and between four and 20 digits afterwards. Note that we do not allow hyphens.