Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Preventing Submit of my HTML form?!

Posted on 2004-10-18
4
Medium Priority
?
195 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

647 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