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.


function eCheck()
{
 var filePath = document.getElementsByName("fileSelect");

 if (filePath == '')
 {
  errorPath();
  return false;
 }

  return true;
}


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


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

probably want to do something liek this:


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

also, getelementsbyname is depreciated.

you should use getElementById(“fileSelect”) and ID your form appropriately.

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?

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…

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.

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;

forget everything else. Alright i want it to check and return false.


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

  return status;
 }
<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.


function status()
 {
   var status = false;
   return status;
 }

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

yes i did type Id not id, i made a mistake while i re-typed it here.

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.


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

or,


var filepath = document.getElementById("fileSelect").value;

function errorpath() {
  if (something) { return true; }
else { return false; }
}

if((filepath == "") || (isNull(filepath)) { 
   return errorpath();
 }

in case you missed it, its singluar as well, element, not elementS

briansol, I’ll try your suggestions as soon as i get home :slight_smile:

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.

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;
  }
}
<form action="process.php" method="post" name="upload">

outside form

</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?