Below is the code where I am trying to Validate that the form is not empty and that the user typed his or her name in only in uppercase letters and send the form to a server program.
I got the uppercase validation right but but not the empty part,if the form is empty I get the enter only uppercase message.
<!DOCTYPE html>
<html>
<head>
<title>form validation</title>
<script type='text/javascript'>
function formValidator(){
// Make quick references to our fields
var firstname = document.getElementById('firstname');
// Check each input in the order that it appears in the form!
if(isAlphabet(firstname, "Please enter only uppercase letters for your name")){
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[A-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
</head>
<body>
<form onsubmit='return formValidator()' >
Name: <input type='text' id='firstname' />
<input type='submit' value='Submit' />
</form>
</body>
</html>
This post has been edited by Dormilich: 06 December 2012 - 05:35 AM
Reason for edit:: please use [CODE] [/CODE] tags when posting code

New Topic/Question
Reply


MultiQuote





|