I have this code and it should display message if the characters are less than 5
ID: 3715205 • Letter: I
Question
I have this code and it should display message if the characters are less than 5 and greater than 15. However, if I type a 3 letter word or 16 letter word and click submit, nothing happens. What wrong with the code? Please help.
<!DOCTYPE html>
<html>
<head>
<script>
function validationForm()
{
var a = document.forms.name.value;
if(a=="")
{
alert("Please enter your name:");
document.forms.name.focus();
return false;
}
if(!isNaN(a))
{
alert("Please enter only characters");
document.forms.name.select();
return false;
}
if ((a.length < 5) || (a.length > 15))
{
alert("Your Character must be 5 to 15 characters");
document.forms.name.select();
return false;
}
}
</script>
</head>
<body>
<form name="form" method="post">
<tr>
<td>Your Name:</td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub" value="Submit"></td>
</tr>
</form>
</body>
</html>
Explanation / Answer
The code is correct but you have to make some changes.
Please refer the modified code below.
<html>
<head>
<script>
function validation()
{
var a = document.form.name.value;
if(a=="")
{
alert("Please enter your name:");
document.form.name.focus();
return false;
}
if(!isNaN(a))
{
alert("Please enter only characters");
document.form.name.select();
return false;
}
if ((a.length < 5) || (a.length > 15))
{
alert("Your Character must be 5 to 15 characters");
document.form.name.select();
return false;
}
}
</script>
</head>
<body>
<form name="form" method="post">
<tr>
<td>Your Name:</td>
<td> <input type="text" name="name"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="sub" value="Submit"></td>
</tr>
</form>
</body>
</html>