Docsity

form validation tutorial, Exercises of Internet and Information Access

Toggle sidebar
bg1
Experiment #04 Form Validation
Instructions
Special Notes:
Please remember that the following JavaScript functions can be done by using HTML5 as well
but the functions in HTML5 are not faultless therefore still JavaScript are used proper
validations.
Form validation
When a user enters data to a form and submits it we can use JavaScript to validate these data
before sending it the server for further processing.
JavaScript can be used to check whether;
• A text field is empty
• User has entered numbers when string is expected and vice versa
• Email address is valid etc…
In validating html 5 forms there are several ways of retrieving the values from the html page.
• To get the data by the element by ID
• To get the data by the DOM object
Experiment:
Step 1 – Create the following form
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download form validation tutorial and more Exercises Internet and Information Access in PDF only on Docsity!

Experiment #04  Form Validation

Instructions

Special Notes:

Please remember that the following JavaScript functions can be done by using HTML5 as well

but the functions in HTML5 are not faultless therefore still JavaScript are used proper

validations.

Form validation

When a user enters data to a form and submits it we can use JavaScript to validate these data

before sending it the server for further processing.

JavaScript can be used to check whether;

  • A text field is empty
  • User has entered numbers when string is expected and vice versa
  • Email address is valid etc…

In validating html 5 forms there are several ways of retrieving the values from the html page.

  • To get the data by the element by ID
  • To get the data by the DOM object

Experiment:

Step 1 – Create the following form

Use the following name and id attributes for the text fields of the above form.

HTML element name/ id

Text field 1 fname

Text field 2 address

Text field 3 zip

Combo box state

Text field 4 username

Text field 5 email

Note : make sure you write the form tag as follows.

Note that formValidate() function will be called when the user clicked on submit button when

used with the event onsubmit.

Step 2 – Access form elements and values using JavaScript.

form name

HTML element name

Step 4 – implement isAlphebatic() function

Function name isAlphebatic

Parameters elemValue : the value of the HTML element

Output returns true if the value is composed of Alphabetic characters

returns false otherwise

Step 5 – implement isAlphaNumeric() function

Function name isAlphaNumeric()

Parameters elemValue : the value of the HTML element

Output returns true if the value is composed of AlphaNumeric characters

returns false otherwise

Step 7 – implement selectedState() function

Function name selectedState

Parameters elemValue : the value of the HTML element

Output returns true if the user has chosen a state from the combo box

returns false otherwise

Step 8 – implement userValidation() function

Function name userValidation

Parameters elemValue : the value of the HTML element

min : minimum number of characters the username can have

max : maximum number of characters the username can have

Output returns true if the username length is in between the max & min.

returns false otherwise

Step 10 – call the sub functions from the formValidate () function

Exercise:

Take the form that you created in the lab-sheet 2 and do all the necessary validations. The

password validation must be done as you prefer using the knowledge that was taught in the

lab-sheet.

Ensure that the following validation are covered.

1. All the required fields must be submitted.

2. First Name & Last Name must be consist of Alphabetical characters only.

3. Date of Birth is chosen from a calendar.

4. Username should be a valid email address.

5. Password must be at least 8 characters long and must be a combination of uppercase

characters, lowercase characters, numeric characters and a special character.

6. Upon submission, the password and the confirm password should be an exact match and

the user should be asked to refill the password/confirm password otherwise with proper

error message.

7. Country should be selected from a given list (drop down list)