DelphiFAQ Home Search:

Validating user input in a form

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

How can I validate a form's user input?

Answer:

You can either put an onblur event handler on each input field (text input, radio buttons, check boxes, select lists) which will get triggered each time a user leaves any field. This is useful if there are many input fields on the screen that can be evaluated independently.

If you either have only a few fields or if some of the fields' rules depend on other fields, then you might prefer to validate upon submission of the form.
Use the form's onsubmit event for that as the following example shows.

If the validation fails (user puts in an age under 18) then the data does not get posted and the form stays.

<html><body>

<script language="JavaScript">
<!--
function valid(form) {
var field = form.age;  
var userAge = parseInt(field.value);   

	if (!userAge) {
		alert("You must indicate your age.");
		return false;
	} 
	else if (userAge >= 18) {  
		alert("Thank you, you can proceed to our special content.");  
		return true;
	} 
	else {
		alert("You are only " + userAge + ". Try again when you are 18."); 
		field.focus();
		field.select();
		return false;
	}
}
// -->
</script>


<form method="GET" action="http://www.coders-corner.com/" onSubmit="return valid(this)">
Your age: <input type="text" name="age" size="2"><BR>
<input type="submit" value="Tell Age">
</form>

</body></html>

Content-type: text/html

Comments:

2008-02-21, 09:57:07
anonymous  
2015-02-01, 04:04:28
anonymous from Venezuela  
Kudos! What a neat way of thikinng about it.
2015-02-02, 06:03:37
anonymous  
With every thing which appears to be builidng inside this specific subject matter, many of your perspectives are generally rather refreshing. Nonetheless, I am sorry, but I do not subscribe to your entire plan, all be it radical none the less. It looks to me that your comments are not entirely rationalized and in actuality you are yourself not completely certain of your point. In any case I did take pleasure in reading it. http://awowqisp.com [url= http://vdmujv.com]v..mujv[/url] [link= http://coheudc.com]..udc[/link]
2015-02-02, 09:49:27
anonymous from Korea (R)  
*This is the right blog for anyone who wants to find out about this topic. You <a href=' http://tfvbuhs.com'..</a> so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: