I have this code and it should display message if the characters are less than 5
ID: 3715163 • 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
Below i had modified the code and it is working fine.
Hope this helps...
Thankyou...:)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
Your Name:
<input type="text" id="dataField" name="name">
<input type="button" value="Submit" />
<script type="text/javascript">
function validationForm(){
var a = document.getElementById("dataField").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;
}
else{
alert(a);
}
}
</script>
</body>
</html>