Connecting Tech Pros Worldwide Forums | Help | Site Map

JS: What does this code need to validate & submit (not 1 or the other)

Newbie
 
Join Date: Apr 2007
Posts: 7
#1: Apr 9 '07
Hello,
I have been trying to get this JS form validation code to work ad nauseam... it won't validate, stop if required and submit. The best I managed was to get it to display an empty field alert but still submit.

I think the problem lies with the form syntax but I tried different variations and still couldn't get it to work properly; the code is here:
Expand|Select|Wrap|Line Numbers
  1. function formValidator()
  2. {
  3. var firstname = document.getElementById("firstname");
  4. //..rest of the fields}
  5.     if(isEmpty(firstname, "Please enter a value"))
  6.     {//..input check, rest of fields}
  7.                 return true;}
  8.                 return false;}
  9.  
[html]
<form name="theform" id="theform" action="insert_val.php" method="post" onSubmit="return formValidator()">
//..rest of form input with id='firstname'
<input type="button" value="Add" onclick="submit()" />
[/html]
Any suggestions for the syntax to use so the form is checked when submit is pressed, stops submit if there's an empty field then allows me to submit again?

Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#2: Apr 9 '07

re: JS: What does this code need to validate & submit (not 1 or the other)


Quote:

Originally Posted by holy moly

<input type="button" value="Add" onclick="submit()" />
[/html]
Any suggestions for the syntax to use so the form is checked when submit is pressed, stops submit if there's an empty field then allows me to submit again?

Use a submit type input and don't call the submit method.
Newbie
 
Join Date: Apr 2007
Posts: 7
#3: Apr 9 '07

re: JS: What does this code need to validate & submit (not 1 or the other)


Do you mean this:

[html]<input type="submit" value="Add Observations" />[/html]

because that won't let me submit the form even if its ok
Newbie
 
Join Date: Apr 2007
Posts: 7
#4: Apr 9 '07

re: JS: What does this code need to validate & submit (not 1 or the other)


The problem seems to be that even if the JS function finds an empty field and displays an alert box it will submit the form when you click 'ok'.

I need the function to let me correct the error then submit the form

Expand|Select|Wrap|Line Numbers
  1.     if(isEmpty(valp, "Please enter a value"))
  2.     {
  3.         return true;
  4.  
  5. function isEmpty(elem, helperMsg)
  6. {
  7.     if(elem.value.length == 0)
  8.     {
  9.         alert('Please Enter a Value');
  10.         elem.focus();
  11.         return true;
  12.     }
  13.     return false;
  14. }
what changes here will enable this?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#5: Apr 11 '07

re: JS: What does this code need to validate & submit (not 1 or the other)


Quote:

Originally Posted by holy moly

Do you mean this:

[html]<input type="submit" value="Add Observations" />[/html]

because that won't let me submit the form even if its ok

That should let you submit the form. Also, add an onsubmit handler in the form tag which will call your from validation function:
[HTML]<form ... onsubmit="return validate();">[/HTML]
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#6: Apr 11 '07

re: JS: What does this code need to validate & submit (not 1 or the other)


Quote:

Originally Posted by holy moly

The problem seems to be that even if the JS function finds an empty field and displays an alert box it will submit the form when you click 'ok'.

I need the function to let me correct the error then submit the form

Expand|Select|Wrap|Line Numbers
  1.     if(isEmpty(valp, "Please enter a value"))
  2.     {
  3.         return true;
  4.  
  5. function isEmpty(elem, helperMsg)
  6. {
  7.     if(elem.value.length == 0)
  8.     {
  9.         alert('Please Enter a Value');
  10.         elem.focus();
  11.         return true;
  12.     }
  13.     return false;
  14. }
what changes here will enable this?

You should return false if there is an error and true if it passes successfully.
Reply