As a programmer for JScript, Inc., you have been assigned to a team to develop a Web site for IT support requests. The Web designer you are working with is good at the visual aspects of the Web forms, but lacks the knowledge to implement the JavaScript code for form validation. There is a new ticket submission form that needs validation. The data on the form and field names are shown below.Ticket Input Form DataReqDate (request date, required, must be mm/dd/yyyy format)EmpID (employee ID, required, must be 6 alphanumeric characters, starting with a capital letter, followed by 5 numbers)FName (user first name, required, must start with capital letter)LName (user last name, required, must start with capital letter)ProbDesc (problem description, required)Your task is to write the JavaScript code for appropriate input validation for this form. The validation code for the form will be in a function called validateInputForm(form) where the form parameter is an object with access to each form field by the names given in the list for each form. The function will return true if the form input is valid, or false if the form input is invalid. The function will be responsible for issuing appropriate alert messages for any invalid input, and should return after finding the first invalid input and notifying the user. The validation function should use if statements and regular expressions where appropriate to validate the input.
Question
As a programmer for JScript, Inc., you have been assigned to a team to develop a Web site for IT support requests. The Web designer you are working with is good at the visual aspects of the Web forms, but lacks the knowledge to implement the JavaScript code for form validation. There is a new ticket submission form that needs validation. The data on the form and field names are shown below.Ticket Input Form DataReqDate (request date, required, must be mm/dd/yyyy format)EmpID (employee ID, required, must be 6 alphanumeric characters, starting with a capital letter, followed by 5 numbers)FName (user first name, required, must start with capital letter)LName (user last name, required, must start with capital letter)ProbDesc (problem description, required)Your task is to write the JavaScript code for appropriate input validation for this form. The validation code for the form will be in a function called validateInputForm(form) where the form parameter is an object with access to each form field by the names given in the list for each form. The function will return true if the form input is valid, or false if the form input is invalid. The function will be responsible for issuing appropriate alert messages for any invalid input, and should return after finding the first invalid input and notifying the user. The validation function should use if statements and regular expressions where appropriate to validate the input.
Solution
Here is the JavaScript code for the form validation:
function validateInputForm(form) {
// ReqDate validation
var datePattern = /^(0[1-9]|1[0-2])\/(0[1-9]|1[0-9]|2[0-9]|3[01])\/(19|20)\d\d$/;
if (!datePattern.test(form.ReqDate.value)) {
alert("Invalid date format. Please enter in mm/dd/yyyy format.");
return false;
}
// EmpID validation
var empIdPattern = /^[A-Z][0-9]{5}$/;
if (!empIdPattern.test(form.EmpID.value)) {
alert("Invalid Employee ID. It must start with a capital letter, followed by 5 numbers.");
return false;
}
// FName validation
var namePattern = /^[A-Z][a-z]*$/;
if (!namePattern.test(form.FName.value)) {
alert("Invalid First Name. It must start with a capital letter.");
return false;
}
// LName validation
if (!namePattern.test(form.LName.value)) {
alert("Invalid Last Name. It must start with a capital letter.");
return false;
}
// ProbDesc validation
if (form.ProbDesc.value == "") {
alert("Problem Description is required.");
return false;
}
return true;
}
This function first validates the date format using a regular expression. If the date format is invalid, it alerts the user and returns false. It then validates the employee ID, first name, and last name using regular expressions. If any of these are invalid, it alerts the user and returns false. Finally, it checks if the problem description is empty. If it is, it alerts the user and returns false. If all validations pass, it returns true.
Similar Questions
Determine the best validation control for each input: Username (compulsory):Answer 1Display a summary of all of the validation errors on the page:Answer 2Re-type password:Answer 3United States postal code:Answer 4Number of tickets (maximum 20):Answer 5Odd number only:
<script type="text/javascript" > function validate() { var msg; if(document.myForm.userPass.value.length>5){ msg="good"; } else{ msg="poor"; } document.getElementById('mylocation').innerText=msg; } </script> <form name="myForm"> <input type="password" value="" name="userPass" onkeyup="validate()"> Strength:<span id="mylocation">no strength</span> </form>
Design a login form using HTML & JavaScript with following validations usingRegular Expression on username and password fields.1. Username and Password should not be both blank2. Username should not start with digit,_, @ or #3. Password length must be 8 to 16 characters
You have created a form for the company’s web site. After looking at some of the data coming back from the form, you realize that people have not been entering valid email addresses. You need to apply input validation to make sure that users enter proper email addresses. What should you do?1 pointChange the input element type attribute to “email”.You cannot test to ensure the user inputs a valid email in an HTML form. For that you need a script.Add pattern=”[a-Z][0-9][?=.*@]{6,}” to the email input element.Add pattern=”[a-Z]{6,}” to the email input element.
Which of the following can't be done with client-side JavaScript?Question 6Select one:a.Storing the form's contents to a database file on the serverb.None of the abovec.Sending a form's contents by emaild. Validating a form
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.