Jump to content

how to block executing a form if an text field equals none?


aram

Recommended Posts

Give the form a name so you can execute it with javascript. Replace your submit button with a regular button and when you click the button check the inputs. If all checks out then you can submit the form in javascript using the form name. If it doesn't all check out then take whatever actions to informing the user of the problems.HTML

<form name="myform" action="..." method="post">inputs ...<input type="button" onclick="checkMyForm()"/></form>

Javascript

function checkMyForm(){form = document.forms["myform"];if(conditions == true)   form.submit();else{   //Show errors}

Link to comment
Share on other sites

The <form> element doesn't have a name attribute according to the HTML 4.01 and XHTML 1.0 standard. You should give the form an ID attribute and refer to it with the getElementById() method.document.getElementById("form")

Link to comment
Share on other sites

If a form contains a text input, users expect the form to be submitted if they hit enter/return while a text input has the focus. This is default behavior. In such a case, they should not be required to click a button. For this reason, it is better to have a true submit button, and to attach your event handler to the form's submit event.

Link to comment
Share on other sites

Its not working here is my form:

<form method='POST' action='new.php' enctype='multipart/form-data' name='check'>";<input type='text' name='title'  size='75'>";

and here is how i add my java:

<script type="text/javascript">	function checkMyForm(){form = document.forms["check"];if(document.input["title"] !== "")   form.submit();else{   //the text is null}</script>

is that wrong?

Link to comment
Share on other sites

Dot syntax is old-fashioned and can be tricky to get just right. If you want a reference to your input, give it an ID and reference it using document.getElementById() . Do that for your form also. Ingolme said you should do this. Take advice that we give you, please. :)To test the content of a form input, reference its value property.An event handler should return a true or false value. Return false if you do not want the default event handler to execute. Since you are handling the submit process yourself, return false after calling that method. If you don't, your form will submit twice! I assume you do not want the form to submit if the element is empty, so you probably want the whole function to return false.

Link to comment
Share on other sites

ok i have changed it and its like that but its not working

<script type="text/javascript">	function checkMyForm(){form = document.getElementById("check");serder= document.getElementById("serder");if(serder !== "")   form.submit();else{   //Show errors}</script>

ofcourse and i have added the ids to the form and text input

Link to comment
Share on other sites

You have syntax errors in your code. Check your browser's error console to see what the problem is.
No, I don't think there are any syntax errors. But maybe I'm just blind. :) [EDIT]Ah, yes I am blind. I just tested the code and the error console pointed out the problem. You were right, Ingolme. :)[/EDIT]However, there is one piece of advice that was not followed:
To test the content of a form input, reference its value property.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...