Hi guys,
I need some help here…
I have created a simple JS and HTML NAME form validation and checked box as shown (If anyone can help me about the check box a better and easy way to code would be great).
I would like to use the functions that I have created - can anyone tell me HOW I can use them please? I tried to use like this…
var name = document.getElementById('name').value;
if(name(!notEmpty && !isAlphabet)) {
error_mesg += "\
Please enter your Name";
error_num++;
}
…but it does not work…anyone can give me some idea?
..
..
<script type='text/javascript'>
function formValidation(){
var error_mesg = "Following error found\
";
var error_num = 0;
var name = document.getElementById('name').value;
if(name==""){
error_mesg += "\
Please enter your Name";
error_num++;
}
var terms = !document.getElementById('terms').checked; //Can anyone tell me what is the function of "checked" here for?
if (terms){
error_mesg += "\
Please check the terms and conditions";
error_num++;
}
if(error_num>0){
alert(error_mesg);
elem.focus();
return false;
}else{
//submit form
return true;
}
}
function notEmpty(elem, error_mesg){
if(elem.value.length == 0){
alert(error_mesg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isAlphabet(elem, error_mesg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(error_mesg);
elem.focus();
return false;
}
}
..
..
[HTML FORM]
..
<form name="form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" onsubmit="return formValidation()">
<tr align="left" valign="top>
<td width="20%">NAME:<font color="#CE0000" >*</font></td>
<td width="80%"><input name="name" type="text" id="name" size="30" /></td>
</tr>
<tr><td><input type=checkbox name=terms value='yes'>I agree to terms and conditions </td></tr>
..
..
<input type="hidden" name="action" value="register"/>
<input name="submit" type="submit" value="Proceed" />