Presentation is loading. Please wait.

Presentation is loading. Please wait.

Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.

Similar presentations


Presentation on theme: "Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why."— Presentation transcript:

1 Form Validation

2 Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why form validation is important – Do basic form validation using jQuery including: Ensuring required text fields have information in them Ensuring that at least one of a (required) group of radio buttons was selected – Understand how and when to use ‘ return false ’

3 Random topic: The return false; statement Here is an important piece of code to know: return false; This statement immediately causes the function to end. Any remaining code that is present in the fucntion will not be executed. For example: function doStuff() { var num1=5, num2=5; var sum = num1+num2; return false; //function immediately ends alert(sum); //this statement will NOT be executed } We might ‘ return false ’ in a situation where we have a function that works with a form. For example, BEFORE we do all of the ‘stuff’ required to process the form, we frequently want to make sure the form has been properly filled out. The process of checking a form to make sure that all required fields are properly filled out is called “form validation”. If there is something that is not properly done in the form, we will typically want to let the user know of the problem. But more importantly, we would then want to STOP the remainder of the function dealing with the form from proceeding. For example, if the user forgot to enter their address, we would not want to continue with a function that adds the user to a mailing list. It is at such a point that we would use return false;

4 Example using return false; Suppose we had a form in which we ask the user to enter their address into a text field called ‘ txt Address ’. We would first check to make sure that the field is not empty. If it is empty, we would let them know that they forgot to complete that field. At that point, we would use ‘return false ’ to end the function. function addToMailingList() { var Address = $('#txt Address').val(); if ( Address == '') //if the field is empty { alert('You forgot to enter your address!'); return false; //ENDS the function right here! } //CODE TO ADD THE ADDRESS TO THE DATABASE WOULD GO HERE... //However, if we HAD executed ‘return false’, then the code //we would never reach this point }

5 Form Validation Example A very important skill in creating web forms is the ability to “validate” the form. That is, to ensure that the form has been properly and/or completely filled out. Examples of form validation include: Making sure that the user has filled out all required fields (name, phone number, etc) Making sure that the user has chosen at least one of a series of radio buttons that ask a question that requires an answer. (E.g. “What is your preferred t-shirt size?”) Making sure that the user has not entered inappropriate data in a field. – This is a pretty important one nowadays. One method used by hackers to break into websites is to enter scripting code into a text field and ‘hijack’ the script to access resources. Therefore, for secure websites, all forms are typically validated to make sure that key programming symbols (semicolons, quotes, asterisks and other important characters) are not present where they do not belong. This is why you will sometimes submit a form only to have it come back and say something like: “Please enter only numbers for your phone number. Please do not enter any other characters such as parentheses or dashes”. For our example, let’s write some code to make sure that a user has not left a textfield called ‘ txtName ’ blank. If they did neglect to enter their name, we will output an alert box letting them know of the error, and highlight that textfield on the page. Here is the hypothetical text field: Name? Now our script: var userData = $('#txtName').val(); if ( userData == '' ) //if it is an empty string, this means the user didn’t enter anything { alert('Please be sure to enter your name.'); $('#txtName').css('background-color', 'yellow'); //Highlight the empty field return false; //since the form was incomplete, do not continue to process it }

6 Form Validation Example Let’s write some code to make sure that a user has not left the ‘name’ and ‘ ’ text fields blank. We will also make sure that they chose one option for their ‘t-shirt size’. For all of these cases, we will output an alert box letting them know which item they neglected to complete. We will also highlight the label preceding the input that was missed. The function connected to the form checks to see that the form was properly completed and all required items were filled out. Start by studying the form and then study the script. FILE: form_validation_simple.htm

7 REVIEW: Checking for ‘UN-checked’ Recall our discussion on determining whether or not something such as a checkbox has NOT been checked: If the following code will determine if something IS checked: if ( $('#chkMushrooms').prop('checked') ) alert('You want mushrooms'); Then we can use the ‘!’ operator, to determine if something has NOT been checked: if ( ! $('#chkMushrooms').prop('checked') ) alert('You do NOT want mushrooms');

8 Checking for ‘UN-checked’ with Radio Buttons We’ve discussed how to determine whether or not a checkbox has been checked. But what if we want to do the same thing with a group of radio buttons? In fact, another very common task in form validation is to check to see if the user neglected to check any of a group of radio buttons. Suppose you have the following radio buttons: Preferred T-Shirt Color? Red Blue Green If we want to verify that the user DID choose one of the three buttons, we need to make sure that all three of the buttons are NOT UN-checked. (I realize there are a lot of negatives in that sentence! – So take a moment to make sure you understand what we are trying to do…) Here is the code to see if ALL 3 buttons are unchecked: if ( ! $('#radRed').prop('checked') && ! $('#radBlue').prop('checked') && ! $('#radGreen').prop('checked') ) { alert('You must choose a color!'); $('lblColor').css('background-color', 'yellow'); return false; }

9 More elaborate form validation example Here is a slightly more elaborate example of form validation: FILE: museum_admission_validation.htm


Download ppt "Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why."

Similar presentations


Ads by Google