473,416 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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
5 2322
Logician
210 100+
<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
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
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
16,027 Expert Mod 8TB
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
16,027 Expert Mod 8TB
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

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

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
19
by: Vinod | last post by:
Hi, I have got a peculiar requirement. I want to distinquish between the color codes. I have got two text fields and i will enter the color codes there. The first text field will have ...
3
by: Megha Vishwanath | last post by:
Hi, I have a struts form <html:form> in which although the focus rests on the submit button, hitting the enter key does not submit the form. I tried capturing the Enter key event and followed...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I...
1
by: amerar | last post by:
Hi All, Here is my situation: I have a form where the user can perform a number of tasks. One of these tasks is to enter a new record. The form has an 'onsubmit' property to validate the data...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
5
by: Ted Ngo | last post by:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { txtSource.Focus(); frmMain.Visible = true; divWait.Visible = false; btnsubmit.Visible = true;
2
by: bips2008 | last post by:
The code seems to work fine in other browser but in IE it throws this error. This is very urgent for me and any help would be greatly appreciated For your convienence i have posted the code for the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.