?
Solved

Preventing Submit of my HTML form?!

Posted on 2004-10-18
4
Medium Priority
?
194 Views
Last Modified: 2010-04-09
In my HTML form I have :
<input type=text id="txtSearch" name="txtSearch">
it's a simple text box, but when I input some text after pressing key "Enter" the form submits?!?! Way? I need to validate the entered text and if it is good I will submit the form from JS function, but in this way the form submits every time I press the key "Enter". How to prevent this submit?
Any articles or code examples will be helpfull for me!

Thanks in advance!
Valeri
0
Comment
Question by:Valeri
[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
  • 3
4 Comments
 
LVL 33

Accepted Solution

by:
sajuks earned 500 total points
ID: 12336742
// try this ex.

<script>
function validateTextBox()
     {
     //Make sure the Field has something in it
     if(document.form1.fname.value=="")
     {
          alert("Please enter your First Name"); //Alert the user
          document.form1.fname.focus(); //Focus on the field
          return false; //Error, DO NOT SUBMIT
     }
     return true; //No Error, Submit Form
}
//--End Hide From Old Browsers-->
</script>


</head>

<body>
<form name="form1" method="post" action="" onSubmit="return validateTextBox();">
  Enter your First Name:
  <input type="text" name="fname">
  <input type="submit" name="Submit" value="Validate">
</form>
</body>
</html>
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12336748
your textbox must be enclosed with a form. thats y it was submitting whenevr you pressed enter
your code must've looked like this
<form>
<input type=text id="txtSearch" name="txtSearch">
</form>


0
 
LVL 18

Expert Comment

by:arantius
ID: 12401410
As posted, the (javascript) onsubmit event of the form tag is the way to do this.
Return true from the function called there and the form will submit.  Return false and the form will not submit.
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12612185
Thanks for the points and grade
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Suggested Courses

770 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