Jump to content

How to call two javascript methods onSubmit


jay_bo

Recommended Posts

Hi, i'm trying to get two javascripts to run when my form is submitted but i'm having trouble with the function validate...

 

This is my form tag....

<form name="form1" onsubmit="return formValidator() && validate()">

 

And my javascript

 

<script language="javascript">

<!--

function validate(){

var f=document.form1;

if(formValidator()==true){

f.command.value='update';

f.submit();

}

 

function formValidator(){

// Make quick references to our fields

var firstname = document.getElementById('firstname');

var lastname = document.getElementById('lastname');

var email = document.getElementById('email');

var number = document.getElementById('number');

var street = document.getElementById('street');

var postcode = document.getElementById('postcode');

var town = document.getElementById('town');

var county = document.getElementById('county');

var telephone = document.getElementById('telephone');

var other_telephone = document.getElementById('other_telephone');

var username = document.getElementById('username');

var password = document.getElementById('password');

 

 

// Check each input in the order that it appears in the form!

if(isAlphabet(firstname, "Please enter only letters for your First Name")){

  if(isAlphanumeric(lastname, "Numbers and Letters Only for Last Name")){

if(emailValidator(email, "Please enter a valid email address")){

    if(isAlphanumeric(number, "Numbers and Letters Only for the Property Number/Name")){

      if(notEmpty(street, "Please enter only letters for Street")){

if(notEmpty(postcode, "Numbers and Letters Only for Post Code")){

  if(isAlphabet(town, "Please enter only letters for Town")){

  if(isAlphabet(county, "Please enter only letters for County")){

    if(isNumeric(telephone, "Please enter only numbers for telephone")){

  if(isNumeric(other_telephone, "Please enter only numbers for Alternative Telephone")){

if(lengthRestriction(username, 6)){

  if(lengthRestriction(password, 6)){

return true;

}

}

}

}

}

}

}

}

}

}

}

}

 

return false;

 

}

 

function notEmpty(elem, helperMsg){

if(elem.value.length == 0){

alert(helperMsg);

elem.focus(); // set the focus to this input

return false;

}

return true;

}

 

function isNumeric(elem, helperMsg){

var numericExpression = /^[0-9]+$/;

if(elem.value.match(numericExpression)){

return true;

}else{

alert(helperMsg);

elem.focus();

return false;

}

}

 

function isAlphabet(elem, helperMsg){

var alphaExp = /^[a-zA-Z]+$/;

if(elem.value.match(alphaExp)){

return true;

}else{

alert(helperMsg);

elem.focus();

return false;

}

}

 

function isAlphanumeric(elem, helperMsg){

var alphaExp = /^[0-9a-zA-Z]+$/;

if(elem.value.match(alphaExp)){

return true;

}else{

alert(helperMsg);

elem.focus();

return false;

}

}

 

function lengthRestriction(elem, min, max){

var uInput = elem.value;

if(uInput.length >= min){

return true;

}else{

alert("Please enter " +min+ " or more Characters");

elem.focus();

return false;

}

}

 

function emailValidator(elem, helperMsg){

var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

if(elem.value.match(emailExp)){

return true;

}else{

alert(helperMsg);

elem.focus();

return false;

}

}

//-->

</script>

Link to comment
Share on other sites

validate() doesn't return anything which is requested in the onSubmit event.

Either return something in that function as well or create a wrapper function that checks for both of those 2 functions.

 

function submit_handler() {
  if (formValidator() && validate()) {
    return true;
  }
  return false;
}

 

But then again validate() needs to return something.

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.