SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error check before form submission

    Hello,

    It should be working but for some reasons it's not.

    Code JavaScript:
    function Validator(frmname)
    {
      this.formobj=document.forms[frmname];
    	if(!this.formobj)
    	{
    	  alert("BUG: couldnot get Form object "+frmname);
    		return;
    	}
    	if(this.formobj.onsubmit)
    	{
    	 this.formobj.old_onsubmit = this.formobj.onsubmit;
    	 this.formobj.onsubmit=null;
    	}
    	else
    	{
    	 this.formobj.old_onsubmit = null;
    	}
    	this.formobj.onsubmit=form_submit_handler;
    	this.setAddnlValidationFunction=set_addnl_vfunction;
    	this.clearAllValidations = clear_all_validations;
    }
     
    function set_addnl_vfunction(functionname)
    {
      this.formobj.addnlvalidation = functionname;
    }
     
    function clear_all_validations()
    {
    	for(var itr=0;itr < this.formobj.elements.length;itr++)
    	{
    		this.formobj.elements[itr].validationset = null;
    	}
    }
     
    function form_submit_handler()
    {
    	for(var itr=0;itr < this.elements.length;itr++)
    	{
    		if(this.elements[itr].validationset &&
    	   !this.elements[itr].validationset.validate())
    		{
    		  return false;
    		}
    	}
    	if(this.addnlvalidation)
    	{
    	  str =" var ret = "+this.addnlvalidation+"()";
    	  eval(str);
        if(!ret) return ret;
    	}
    	return true;
    }
     
    function uploaderError()
    {
      var frm = document.forms["upload"];
     
      var uplSplit = frm.filePath.value.match("^(.+).(.+)$");
      if (uplSplit == null) { ErrorPath(); return false; }
     
      if (uplSplit[2] != null )
      {
         if ((uplSplit[2].match("gif") == null) || (uplSplit[2].match("jpg") == null) || (uplSplit[2].match("jpg") == null))
         { ErrorPath(); return false; }
         else { return true; }
      }
     
     
      if (frm.filePath.value == '')
      {
         ErrorPath();
         return false;
      }
      else if ((frm.Title.value == '') || (strlen(frm.Title.value) > 15))
      {
         ErrorTitle();
         return false;
      }
      else if ((frm.height.value != '') && (frm.width.value == ''))
      {
         Errorresize();
         return false;
      }
      else if ((frm.hieght.value != '') && (frm.width.value == ''))
      {
         Errorresize();
         return false;
      }
      else if ((frm.pincode.value != '') && (frm.memid.value == ''))
      {
         ErrorMem();
         return false;
      }
      else if ((frm.memid.value != '') && (frm.pincode.value == ''))
      {
         ErrorMem();
         return false;
      }
      else
      {
         return true;
      }
    }

    Code JavaScript:
    <form action="process.php" method="post" name="upload">

    outside form

    Code HTML4Strict:
    </form>
    <script language="JavaScript" type="text/javascript">
     var frmCheck  = new Validator("upload");
     frmCheck.setAddnlValidationFunction("uploaderError");
    </script>

    form name is "upload". The problem is, it's not going through the checks.

    Thanks in Advace.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could anyone here help? ...is it that impossible to do error check on submit?

  3. #3
    SitePoint Member swatij's Avatar
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    You can put line as
    <form action="process.php" method="post" name="upload" onsubmit="return Validator([parameter]);">

    For which ever form you want to validate.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •