JavaScript validation/code Form Validation: In this assignment there will be 5 t
ID: 3856210 • Letter: J
Question
JavaScript validation/code
Form Validation:
In this assignment there will be 5 things to validate:
1- Validate that the names are filled in, first and last name
2- Validate that they are letters, upper case and lower case. (You can use /^[a-zA-Z]+$/ as the regular expression)
3- Check to make sure the phone number is all numbers. (You can use /^[0-9]+$/ as the regular expression)
4- If they are updating the form make sure the form is filled out completely.
5- If they are creating a new record make sure they filled the form out completely.
If any of these fields are not met then the form will produce an alert and let them know what they are missing.
Explanation / Answer
1.Below function validates first name and displays alert if not valid
function validateFirstName(firstName) {
if (/^[a-zA-Z]+$/.exec(firstName)) {
return true;
}
alert("Please enter valid first name");
return false;
}
2.Below function validates last name and displays alert if not valid
function validateLastName(lastName) {
if (/^[a-zA-Z]+$/.exec(lastName)) {
return true;
}
alert("Please enter valid last name");
return false;
}
3.Below function validates phone number and displays alert if not valid
function validatePhoneNumber(number)
{
var phoneno = /^[0-9]+$/;
if((number.value.match(phoneno))
{
return true;
}
else
{
alert("Please enter valid phone number");
return false;
}
}
4.Below function validates if form is filled completely and displays alert if not valid
function emptyFormCheck(){
var flag = true;
$('form .required').each(function(){
if ($(this).val() == ""){
alert('Please fill value for ' + $(this).attr('name'));
flag = false;
}
});
return flag;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.