Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I prevent a Submit if input validation fails?

Posted on 2010-08-23
7
Medium Priority
?
443 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This article discusses four methods for overlaying images in a container on a web page
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

609 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