Solved

Preventing Submit of my HTML form?!

Posted on 2004-10-18
4
188 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 125 total points
Comment Utility
// 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points and grade
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
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 learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now