Solved

How do I prevent a Submit if input validation fails?

Posted on 2010-08-23
7
413 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
  • 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Ajax control editor 4 34
html input clean up 3 32
Bootstrap input box width 2 15
CSS measurement 10 14
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now