Solved

Preventing Submit of my HTML form?!

Posted on 2004-10-18
4
192 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 125 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Read text on Table 7 47
Keyup listener getting fired on initial page load 1 52
Jquery syntax 12 29
Can't Find The Code 15 14
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.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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