Developing a form using javascript for validation but its not working!

Asked by khushi ahuja on 2012-07-30

I am a beginner in php...developing a form using javascript for validation but its not working! please help...
the code is

<html>
<head><title> Registration Form</title></head>
<body>

<script type='text/javascript'>

function formValidator(){
 // Make quick references to our fields
 var Name = document.getElementById('Name');
 var Department = document.getElementById('Department');
 var Roll_No = document.getElementById('Roll_No');
 var Telephone_No = document.getElementById('Telephone_No');
 var CGPA = document.getElementById('CGPA');
 var Committee = document.getElementById('Committee');
        var Password = document.getElementById('Password');

 // Check each input in the order that it appears in the form!
 if(isAlphabet(Name, "Please enter only letters for your Name")){
            if(isAlphabet(Department, "Please enter only letters for your Department")){
  if(isAlphanumeric(Roll_No, "Numbers and Letters Only for Roll_No")){
   if(isNumeric(Telephone_No, "Please enter a valid Telephone_No")){
                            if(isNumeric(CGPA, "Please enter a your CGPA")){
    if(madeSelection(Committee, "Please Choose a Committee")){
     if(isAlphanumeric(Password, "Numbers and Letters Only for Password")){
       return true;
      }
     }
    }
   }
  }
 }

 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 isNumeric(elem, helperMsg){
 var numericExpression = /^[0-9]+$/;
 if(elem.value.match(numericExpression)){
  return true;
 }else{
  alert(helperMsg);
  elem.focus();
  return false;
 }
}

function isAlphabet(elem, helperMsg){
 var alphaExp = /^[a-zA-Z]+$/;
 if(elem.value.match(alphaExp)){
  return true;
 }else{
  alert(helperMsg);
  elem.focus();
  return false;
 }
}

function isAlphanumeric(elem, helperMsg){
 var alphaExp = /^[0-9a-zA-Z]+$/;
 if(elem.value.match(alphaExp)){
  return true;
 }else{
  alert(helperMsg);
  elem.focus();
  return false;
 }
}

function madeSelection(elem, helperMsg){
 if(elem.value == "Please Choose"){
  alert(helperMsg);
  elem.focus();
  return false;
 }else{
  return true;
 }
}

</script>
<h3> Register here!</h3>
<form onsubmit='return formValidator()' >
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="Name" /></td>
</tr>
<tr>
<td>Department:</td>
<td><input type="text" id="Department" /></td>
</tr>
<tr>
<td>Roll_No:</td>
<td><input type="text" id="Roll_No" /></td>
</tr>
<tr>
<td>Telephone_No:</td>
<td><input type="text" id="Telephone_No" /></td>
</tr>
<tr>
<td>CGPA:</td>
<td><input type="text" id="CGPA" /></td>
</tr>
<tr>
<td>Committee(Choose only 1):</td>
<td><select id="Committee">
<option id="D"> Dance--D </option>
<option id="DR"> Drama--DR </option>
<option id="M"> Music--M </option>
<option id="H"> Hospitality--H </option>
<option id="S"> Stage--S </option>
</select>
</td>
</tr>
<tr>
<td>Password:</td> <td><input type="Password" id="Password" /></td>
</tr>
</table>
<input type='submit' value='Check Form' />
<input type="submit" value="Submit"/>
</form>
<p> Thank you! </p>

</body>
</html>

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu php5 Edit question
Assignee:
No assignee Edit question
Last query:
2012-07-30
Last reply:
2012-08-08
Enrico Rosina (e-rosina) said : #1

Hello,

I think here it's not the right place to ask such a question. I suggest you to look for other forums.
There are plenty of sites you can chose. I could suggest you for instance one of these:

http://www.coderanch.com/forums/f-20/HTML-CSS-JavaScript
http://forums.netbeans.org/php-users.html
http://www.codingforums.com/forumdisplay.php?f=2

I also suggest you to ask your question on a javascript forum, because obviously your question is not
directly related to php :-)

Regards.
Enrico

Can you help with this problem?

Provide an answer of your own, or ask khushi ahuja for more information if necessary.

To post a message you must log in.