Solved

How do I prevent a Submit if input validation fails?

Posted on 2010-08-23
7
424 Views
Last Modified: 2012-05-10
If I'm doing input validation behind a HTML submit button, and the validation fails, how do I prevent the "submit"?  Doing my input validation in the click of the submit button, is this the "correct" way to do it?

Thanks.
0
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33504621
Many ways to do this.

One way is to use the onFocus() event.

This way, if the box is blank or doesn't have the correct info, the focus stays inside the box.

<SCRIPT LANGUAGE=javascript>
function validate(theForm){
      if (theForm.mailingname.value.replace(/\s/g,"").length < 8) {
            alert("Please fill in your mailing name.");
            theForm.mailingname.focus();
            return false;
      }

      if (theForm.address.value.replace(/\s/g,"").length < 8 ){
            alert("Please fill in your address.");
            theForm.address.focus();
            return false;
      }
</script>

I am sure there are better ways.
0
 
LVL 10

Accepted Solution

by:
Jini Jose earned 500 total points
ID: 33504638
suppose validatePage() is your javascript function and it should return true or false.

then in your button onclick event you have to put onclick="return validatePage();"

then if the function returns false then your submit never hits.
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 33504643
Yea, but I need to check a series of input fields.  I want to check when they click Submit.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 33504661
Perfect.  I'm new to this.  I did not realize just returning false would prevent the submit.

Thanks!
0
 
LVL 3

Expert Comment

by:arthurmnev
ID: 33504700
The solution above makes sense with a "BUT"

Some browsers that don't have java script turned on will ignore the return value and submit either way. If you want to bypass it , several options are available to you:

1) Create a button, but do not make a submit button, use form.submit from within the validation function itself. That will ensure that if the form submits, javascript actually ran (and if the browser ignores javascript, the button will do nothing)

2) Use server side validation, depending on the back-end used, you'd use different methodologies (i.e. redirects, transfers, session variables etc...)

0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33504734
arthurmnev is absolutely write.
but now a days, i think javascript is enabled in most of the browsers.

we also doing the same way.. first javascript validation then code behind validations.
apart from this you have to do also the database checks.
0
 
LVL 3

Expert Comment

by:arthurmnev
ID: 33505438
Javascript is enabled in most places unless:

1) Web browser is locked down (some corporate / government locations)
2) Poorly supported javascript (i.e. mobile devices ...[ further -- blackberries with browsers not based on web kit 3))

From my perspective, I try to incorporate server side as much as possible. If I have to use javascript for anything other than UI management, I seek to go Ajax (minimal, pretty much bulletproof code) and have server side execute the logic, or go to Page.Transfer / Page.Redirect in Microsoft world.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add a little space on either side of a table. 12 25
replacing inline javascript with jquery 4 46
Html using "Or" in condition 3 29
Validating number not work with decimal 4 29
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question