Solved

How do I prevent a Submit if input validation fails?

Posted on 2010-08-23
7
429 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 29

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

696 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