By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

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

P: 7
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?
Apr 8 '07 #1
Share this Question
Share on Google+
5 Replies


100+
P: 210
<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.
Apr 9 '07 #2

P: 7
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
Apr 9 '07 #3

P: 7
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?
Apr 9 '07 #4

acoder
Expert Mod 15k+
P: 16,027
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]
Apr 11 '07 #5

acoder
Expert Mod 15k+
P: 16,027
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.
Apr 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.