Saturday, July 9, 2016

js validation in a form and set focus


<html>
<head>
<script type="text/javascript">

function validate(){
var eduInput = document.getElementsByName('edu[]');
var name4=document.getElementById('name4').value;
for (i=0; i<eduInput.length; i++) {
if (eduInput[i].value == "")
{
alert('Complete all the fields');
document.getElementById(eduInput[i].id).focus();
return false;
}
}
if(name4==''){
alert('Enter name field');
document.getElementById('name4').focus();
return false;
}
}

</script>
</head>
<body>
<form onsubmit="return validate()">
Education:<br>
<input type="text" name="edu[]" id="name1" placeholder="name array fields" /><br>
<input type="text" name="edu[]" id="name2" placeholder="name array fields" /><br>
<input type="text" name="edu[]" id="name3" placeholder="name array fields" /><br>
<input type="text" name="edu" id="name4" placeholder="name" /><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

No comments:

Post a Comment