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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.