Home / code

Check a form

Writer: Christophe Wolfs
Published: 27 March 2008

This is a script for checking if a form is filled in corectly.
If it is not filled in corectly it returns a message asking to fill in a certain part in.

<SCRIPT language="JavaScript">
<!-- start
//This is a script for checking if a form is filled in corectly.
//If it is not filled in corectly it returns a message asking to fill in a certain part in
//Code written by Christophe Wolfs http://www.webmaster2020.com

function Check()
{
  j_email = document.form1.email.value;
  j_url = document.form1.url.value;
  j_title = document.form1.title.value;
  j_description = document.form1.description.value;
  j_banner = document.form1.banner.value;
  j_b_height = document.form1.b_height.value;
  j_b_width = document.form1.b_width.value;
  j_username = document.form1.username.value;
  j_password = document.form1.password.value;

  j_pass=0;
  j_what="banner size"

  if (j_email=="" || j_email.indexOf("@")<0  || j_email.indexOf(".")<0)
  {
        j_pass=1;
        j_what="email";
  }

  if (j_url=="" || j_url.indexOf("http://")<0 || j_url.indexOf(".")<0)
  {
        j_pass=1;
        j_what="url";   
  }

  if (j_title=="")
  {
        j_pass=1;
        j_what="title"; 
  }

  if (j_description=="")
  {
        j_pass=1;
        j_what="description";   
  }

  if (j_b_height<0 || j_b_height=="") j_pass=1;
  if (j_b_width<0 || j_b_width=="") j_pass=1;

  if (j_username=="")
  {
        j_pass=1;
        j_what="username";      
  }
  if (j_password=="")
  {
        j_pass=1;
        j_what="password";      
  }

  if(j_pass != 0)
  {
        alert("Not everything is filled in correctly.\nPlease fill in " +j_what+ " corectly.");
        return false;
  }
  else
  {
        return true;
  }
}
// end -->
</SCRIPT>


<FORM method=post action="topage.asp" name=form1  onSubmit="return Check()">
<table>
  <tr>
        <td>Email:</td>
        <td><INPUT TYPE="text" NAME="email" SIZE=25 MAXLENGTH=35></td>
  </tr><tr>
        <td>Url of your website:</td>
        <td><INPUT TYPE="text" NAME="url" SIZE=25 MAXLENGTH=49 value="http://"></td>
  </tr><tr>
        <td>Title of your site:</td>
        <td><INPUT TYPE="text" NAME="title" SIZE=25 MAXLENGTH=35></td>
  </tr><tr>
        <td>Short Discription:</td>
        <td><INPUT TYPE="text" NAME="description" SIZE=25 MAXLENGTH=239></td>
  </tr><tr>
        <td>Url of your banner</td>
        <td><INPUT TYPE="text" NAME="banner" SIZE=25 MAXLENGTH=49></td>
  </tr><tr>
        <td>Banner height:</td>
        <td><INPUT TYPE="text" NAME="b_height" SIZE=6 MAXLENGTH=5 value=0></td>
  </tr><tr>
        <td>Banner width:</td>
        <td><INPUT TYPE="text" NAME="b_width" SIZE=6 MAXLENGTH=5 value=0></td>
  </tr><tr>
        <td>Username:</td>
        <td><INPUT TYPE="text" NAME="username" SIZE=25 MAXLENGTH=23></td>
  </tr><tr>
        <td>Password:</td>
        <td><INPUT TYPE="password" NAME="password" SIZE=25 MAXLENGTH=23></td>
  </tr><tr>
        <td colspan=2> </td>
  </tr><tr>
        <td colspan=2><INPUT type=submit></td>
  </tr>
</table>

</form>

 

TOP