Jump to content

Javascript form validation


lucy

Recommended Posts

Im trying to make a contact validation form using javascript, but i cant figure out what is wrong with it.

 

all fields are required and it works only for the top two fields, i.e. when email and name are not filled out, a popup box apears, but when the subject and message are not filled out, it does not come up with a popup box, even though ive written the code.

 

Here is the code :

<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">

// CSS goes here

</style>
<script language="JavaScript">
function checkForm()
{
   var cname, cemail, csubject, cmessage;
   var regex;
   
   with(window.document.msgform)
   {
      cname    = sname;
      cemail   = email;
      csubject = subject;
      cmessage = message;
   }

   if(trim(cname.value) == '')
   {
      alert('Please enter your name');
      cname.focus();
      return false;
   }
   else if(trim(cemail.value) == '')
   {
      alert('Please enter your email');
      cemail.focus();
      return false;
   }
   else if(!isEmail(trim(cemail.value)))
   {
      alert('Email address is not valid');
      cemail.focus();
      return false;
   }
   else if(trim(csubject.value) == '')
   {
      alert('Please enter message subject');
      csubject.focus();
      return false;
   }
   else if(trim(cmessage.value) == '')
   {
      alert('Please enter your message');
      cmessage.focus();
      return false;
   }
   else
   {
      cname.value    = trim(cname.value);
      cemail.value   = trim(cemail.value);
      csubject.value = trim(csubject.value);
      cmessage.value = trim(cmessage.value);
      return true;
   }
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}



</script>
</head>
<body>
<form method="post" name="msgform">
<table width="500" border="0" align="center" cellpadding="2" cellspacing="1" class="maincell">
<tr>
<td width="106">Your Name</td>
<td width="381"><input name="sname" type="text" class="box" id="sname" size="30"></td>
</tr>
<tr>
<td>Your Email</td>
<td>
<input name="email" type="text" class="box" id="email" size="30">
</td></tr>
<tr>
<td>Subject</td>
<td><input name="subject" type="text" class="box" id="subject" size="30"></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="message" cols="55" rows="10" wrap="OFF" class="box" id="message"></textarea></td>
</tr>
<tr align="center">
<td colspan="2"><input name="send" type="submit" class="bluebox" id="send" value="Send Message" onClick="return checkForm();"></td>
</tr>
<tr align="center">
<td colspan="2"> </td>
</tr>
</table>
</form>

</body>
</html>

 

Thanks,

Lucy

Link to comment
https://forums.phpfreaks.com/topic/170077-javascript-form-validation/
Share on other sites

your javascript is failing at this point

  else if(!isEmail(trim(cemail.value)))

  {

      alert('Email address is not valid');

      cemail.focus();

      return false;

  }

remove that and it validates all the fields. jut check that if statement and the isEmail out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.