473,657 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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="retur n 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 2334
Logician
210 New Member
<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
holy moly
7 New Member
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
holy moly
7 New Member
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 Recognized Expert Moderator MVP
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="retur n validate();">[/HTML]
Apr 11 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
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
6220
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 full name is required. Please enter your full name!'); validity = false; frmComments.name.focus();
19
3234
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 background color The second will have text color. I want to validate if background color is dark then only light colors should
3
11671
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 it by the document.form.submit() but this takes me through the validation attached with the submit twice( giving me all the alerts twice). Here's the code
7
3594
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> </head> <style type="text/css">
4
3500
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 click a button on a pop-up window the javascript for that button click does a 'button.form.submit'. On the Server side there is a Button click event for this button, but for some reason it no longer fires. It worked fine before and everything...
1
1349
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 if the users enters a new record. However, there is also a button to allow the user to 'duplicate' a stored record. In that case, I do not want to perform the entire validation since most of the data will be fetched from the database
0
2252
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 assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
5
1642
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
4293
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 file. The portion that is not working is the star rating part <?php session_start(); include_once('includes/host_conf.php'); include_once('includes/db_connect.php'); include_once('includes/mysql.lib.php');...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.