PHP 7: Adjust Example 4-2 to validate the gender text box from the first Do It.
ID: 3904142 • Letter: P
Question
PHP 7:
Adjust Example 4-2 to validate the gender text box from the first Do It. Make sure to include an error message to include the problem(s). Also make sure the error message is added to the $error_message property to be displayed with all other error messages. Make sure to test your code for all possible problems with the user entering data. Don’t forget to test if they do not enter any data.
Example 4-2:
function allalphabetic(the_string)
{
var letters = /^[a-zA-Z ]+$/;
if (the_string.match(letters))
{
return true;
}
else
{
return false;
}
}
function validate_dog_name(the_string)
{
if ((the_string.length > 0) && (allalphabetic(the_string)) && (the_string.length <= 20))
{
return true;
}
else
{
return false;
}
}
function validate_dog_breed_color(the_string)
{
if ((the_string.length > 0) && (allalphabetic(the_string)) && (the_string.length <= 35))
{
return true;
}
else
{
return false;
}
}
function validate_dog_weight(the_string)
{
if ((the_string > 0 && the_string <=120) && (!isNaN(the_string)))
{
return true;
}
else
{
return false;
}
}
function validate_input(form)
{
var error_message = "";
if (!validate_dog_name(form.dog_name.value))
{
error_message += "Invalid dog name. ";
}
if (!validate_dog_breed_color(form.dog_breed.value))
{
error_message += "Invalid dog breed. ";
}
if (!validate_dog_breed_color(form.dog_color.value))
{
error_message += "Invalid dog color. ";
}
if (!validate_dog_weight(form.dog_weight.value))
{
error_message += "Invalid dog weight. ";
}
if (error_message.length > 0)
{
alert(error_message);
return false;
}
else
{
return true;
}
}
Explanation / Answer
function allalphabetic(the_string) { var letters = /^[a-zA-Z ]+$/; if (the_string.match(letters)) { return true; } else { return false; } } function validate_dog_name(the_string) { if ((the_string.length > 0) && (allalphabetic(the_string)) && (the_string.length 0) && (allalphabetic(the_string)) && (the_string.length 0 && the_string 0) && (allalphabetic(the_string)) && (the_string.toLowerCase() == 'female' || the_string.toLowerCase() == 'male')) { return true; } else { return false; } } function validate_input(form) { var error_message = ""; if (!validate_dog_name(form.dog_name.value)) { error_message += "Invalid dog name. "; } if (!validate_dog_breed_color(form.dog_breed.value)) { error_message += "Invalid dog breed. "; } if (!validate_dog_breed_color(form.dog_color.value)) { error_message += "Invalid dog color. "; } if (!validate_dog_weight(form.dog_weight.value)) { error_message += "Invalid dog weight. "; } if (!validate_dog_gender(form.dog_gender.value)) { error_message += "Invalid dog gender. "; } if (error_message.length > 0) { alert(error_message); return false; } else { return true; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.