SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

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

    onSubmit, run a javascript, return true or false

    hello,

    i want to check for errors on submitting a form, on encountering an error it runs a function and returns a false preventing form submit. I have implemented that.

    Code javascript:
    function eCheck()
    {
     var filePath = document.getElementsByName("fileSelect");
     
     if (filePath == '')
     {
      errorPath();
      return false;
     }
     
      return true;
    }

    Code HTML:
    <form action="process.php" method="post" onSubmit="return eCheck();">

    When i do the above, it isn't going through the check or rather not running the function errorPath(); and returning the value as false.

    but when i do the following, errorPath(); works and form submit is called off.

    Code HTML:
    <form action="process.php" method="post" onSubmit="errorPath(); return false;">

    but i want it to run the error check. Any ideas on way it's not working.

    Thanks in Advance.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2003
    Location
    CT
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    probably want to do something liek this:


    Code:
    function eCheck()
    {
     var filePath = document.getElementsByName("fileSelect");
     
     if (filePath == '')
     {
        if(!(errorPath())) { 
        return false;
        }
      }
    }
    My Blog: SkeyMedia.com

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by briansol View Post
    probably want to do something liek this:


    Code:
    function eCheck()
    {
     var filePath = document.getElementsByName("fileSelect");
     
     if (filePath == '')
     {
        if(!(errorPath())) { 
        return false;
        }
      }
    }
    I think you understood it wrongly. I want to both run errorPath() and return false if filePath is empty.

    As Raffles said, i have names for all from controls. That's why i used getElementsByName(); also i tried the following as a experiment as well..

    Code JavaScript:
    function eCheck()
    {
     var filePath = document.getElementsByid("fileSelect");
     
     if (filePath == '')
     {
      alert('file path is not valid, please correct it');
      return false;
     }
     
      return true;
    }

    It doesn't work.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    forget everything else. Alright i want it to check and return false.

    Code JavaScript:
    var filePath = document.getElementsByName("fileSelect");
    function status()
     {
        var status == none;
       if (filePath == none)
       {
        status = false;
       }
     
      return status;   
     }

    Code HTML4Strict:
    <form action="process.php" method="post" onSubmit="return status();">

    not working, it's not running the 'if' check. but doing the flowing way without check works.

    Code JavaScript:
    function status()
     {
       var status = false;
       return status;   
     }

    the problem is, i want it to run the checks and not manually supply false or true.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2003
    Location
    CT
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, getelementsbyname is depreciated.

    you should use getElementById("fileSelect") and ID your form appropriately.
    My Blog: SkeyMedia.com

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure that isn't true. The name attribute is deprecated for only some elements and only in XHTML 1.0. It's perfectly valid in HTML 4.01 and still valid for form controls in XHTML 1.0 (but not for anchors or AREA elements). Also, getElementsByName is part of the DOM Level 2 spec and I can't see anything to indicate it being deprecated (not depreciated). If form controls are going to have a name attribute anyway, why not use that instead of adding an extra attribute (ID) just to target the node with javascript?

  7. #7
    SitePoint Guru
    Join Date
    Aug 2003
    Location
    CT
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you have a couple typos in this.:
    var filePath = document.getElementsByid("fileSelect");

    should be:
    var filePath = document.getElementById("fileSelect");

    JS is case sensitive.

    and, you probably want to check for a value

    var filePath = document.getElementById("fileSelect").value;
    My Blog: SkeyMedia.com

  8. #8
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by briansol View Post
    you have a couple typos in this.:
    var filePath = document.getElementsByid("fileSelect");

    should be:
    var filePath = document.getElementById("fileSelect");

    JS is case sensitive.

    and, you probably want to check for a value

    var filePath = document.getElementById("fileSelect").value;
    yes i did type Id not id, i made a mistake while i re-typed it here.

  9. #9
    SitePoint Guru
    Join Date
    Aug 2003
    Location
    CT
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cssExp View Post
    yes i did type Id not id, i made a mistake while i re-typed it here.
    in case you missed it, its singluar as well, element, not elementS
    My Blog: SkeyMedia.com

  10. #10
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    briansol, I'll try your suggestions as soon as i get home

  11. #11
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will try what you suggested but before that, my twin wrote a script. 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. Maybe i should post this as a different thread, since the script is different from mine?

  12. #12
    SitePoint Guru
    Join Date
    Aug 2003
    Location
    CT
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it does nothing because your filepath var IS nothing.

    you have 2 things wrong:
    -none is not a keyword.
    -you are referencing an element of your form... but not what's IN the form.

    Code:
    var filepath = document.getElementById("fileSelect").value;
    if((filepath == "") || (isNull(filepath)) { 
       return false;
     }
    or,


    Code:
    var filepath = document.getElementById("fileSelect").value;
    
    function errorpath() {
      if (something) { return true; }
    else { return false; }
    }
    
    if((filepath == "") || (isNull(filepath)) { 
       return errorpath();
     }
    My Blog: SkeyMedia.com


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
  •