?
Solved

How do I prevent a Submit if input validation fails?

Posted on 2010-08-23
7
Medium Priority
?
432 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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 Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

762 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