jQuery in Action, 3rd edition
The moose likes HTML, CSS and JavaScript and the fly likes Validate a form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Validate a form" Watch "Validate a form" New topic

Validate a form

Jeff Dube
Ranch Hand

Joined: Jun 14, 2001
Posts: 44
I do have a form and i need to make sure a value has been enterred in the field. If a field is empty, i use the Alert dialog box to inform the user.
here is my problem; My function is call on load so the alert box is shown on load. i have a "submit" button and i need to call my function whithin this button. How can i
1) Call a function with my button
2) Check at each field?
Thank you, i am new to javascript so excuse me for this "novice" question!!!
Here is an example:
<form method="post">
Enter your first name :
<input type="text" name="something" size="25">
Enter your last name :
<input type="text" name="secondsomething" size="25">
Enter your favorite color :
<input type="text" name="thirdsomething" size="25">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
Thanks Jeff
Jeff Dube
Ranch Hand

Joined: Jun 14, 2001
Posts: 44
For my button, i know the onclick method but i am unable to call a function with it. I guess ;
but do i have to pass my 3 field like parameters...?
Chris Treglio
Ranch Hand

Joined: Jun 18, 2001
Posts: 64
you can use the onSubmit method of the Form object to execute something before you complete the submission. The nice thing about this method is that if it returns false, the form is prevented from submitting. Do something like this
<form method="post" onSubmit="return validateMe(this)">
and then have a function called 'validateMe' that takes a Form object. (The 'this' keyword passes the form object, since it's used in the form tag.)
like this
validateMe (formobj){
if (.....) // check the contents of the form for validity
return true;
return false;
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
to call a functionw hen the form is submitted use the onsubmit event of the form tag:
<form name=myform action=whatever onsubmit='return validate()'>
then in your validate function you can do whatever checking you need to do and then return true or false depending on whether or not the validation was ok or not.
To check the value of each field you can do several things depending on what it is your checking for.
If your just checking for values then you can use someting like this and just reference the name of the text field
if (document.formName.textfield1.value=="")
alert("enter text in the 1st form field");
You would use that way to check each text field for a specific entry also.
Another way would be to loop through all of the elements in the page and check them (if you have a lot elements and only want to check a few of them this may not be the most efficient way)
for (i=0; i<document.formName.elements.length; i++){
if (document.formName.elements[i].type=="text" && document.formName.elements[i].value=="")
Hope that helps


[This message has been edited by Dave Vick (edited June 19, 2001).]

I agree. Here's the link: http://aspose.com/file-tools
subject: Validate a form
jQuery in Action, 3rd edition