?
Solved

Preventing Submit of my HTML form?!

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
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…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Suggested Courses

807 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