How to Check for Passed Values in JavaScript
Values in JavaScript can either be statements or numbers. They are contained within containers called variables. These values can change depending on a number of factors. Thus, while checking for passed values in JavaScript, you use these containers or variables to determine what they contain. Containers are created or declared in JavaScript using the “var” command. Checking for passed values in JavaScript is done by seeing if the correct values have been inputted in form fields (form validation)--for example, whether the correct date format has been used.
Instructions
-
-
1
Create a variable. This is done using the “var” keyword followed by the name you want the variable to take as the example below shows:<br /><br />Var gender<br />GO<br /><br />This is called variable declaration.
-
2
Assign a value to the variable. Use the assignment operator “=” to assign a value to the declared variable as follows:<br /><br />gender = “male”<br />GO<br /><br />In a process called initializing the variable, you declare a variable and assign a value in one statement as follows:<br /><br />var gender = “male”;
-
-
3
Initialize another variable called “year” as follows:<br /><br />var year = 2010
-
4
Create the file below and name it “people.html”:<br /><br /><SCRIPT Language = “JavaScript”><br /><!—<br /><br />function validate() {<br />if (document.info.gender.value ==””)<br />{ <br />alert (“Please input the appropriate gender.”)<br />return false<br />}<br />if (document.info.year.value==””)<br />{<br />alert(“Please input current year.”)<br />return false<br />}<br />}<br />//--><br /></SCRIPT>
-
5
Set the “Gender” and “Year” values appropriately. For example, inside the JavaScript code you could set these values to read as follows:<br /><br /><SCRIPT Language = “JavaScript”><br /><!—<br /><br />function validate() {<br />if (document.info.gender.value ==”male”)<br />{ <br />alert (“Please input the appropriate gender.”)<br />return false<br />}<br />if (document.info.year.value==”2005”)<br />{<br />alert(“Please input current year.”)<br />return false<br />}<br />}<br />//--><br /></SCRIPT>
-
1
Tips & Warnings
The function “return validate” is performed when the “Submit” button is pressed. In this case, it takes you to the link http://www.validate.com/validate when the right values are input.<br /><br />The above example uses what is called DOM (Document Object Model) since it references objects inside other objects. There are other methods that can likewise be employed using JavaScript.
References
- “Intro To JavaScript for Non-Programmers”; XtraNet; 1999