Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Input Boxes

Status
Not open for further replies.

ptuck

MIS
Aug 8, 2003
130
US
First, let me say I am new to JavaScript...

I have four fields that I need to check to see if they are blank when submit button is clicked. If so, then pop up a alert box telling user to populate fields.

Here is my form:
<FORM action="SaveData.asp" method=POST>
<table>
<tr><TD>First Name</td><td><Input Type="text" name="FName" style="width:200;"></TD>
<TD>Last Name</td><td><Input Type="text" name="LName" style="width:240;"></TD></tr>
<tr><TD>Phone</td><td><Input Type="text" name="Phone" style="width:200;"></TD>
<TD>Email</td><td><Input Type="text" name="Email" style="width:240;"></TD></tr>

Could some one please provide me some code examples to solve this problem? Of course, now when submit is clicked it is not using any event.
 
Do a keyword search on this forum (2nd tab at the top), type "form validation" in the EXACT PHRASE box and you'll get about 100 responses. You should find what you're looking for there.

There's always a better way. The fun is trying to find it!
 
Code:
<FORM action="SaveData.asp" method=POST [COLOR=green]onsubmit="return isValid(this);"[/color]>
...
[COLOR=green]
<script>
function isValid(f){
   if ( f.FName.value == "" ) {
      alert("Please enter a first name.");
      f.FName.focus();
      return false;
   }
   ...
   return true;
}
</script>

[/color]
 
That code only checks the first name. Try this:
Code:
<script>
<!--
function verify() {
obj = document.forms[0];  // Assuming this is the first form on the page
	if (obj.FName.value == "") {
		err = obj.FName;
	} else if (obj.LName.value == "") {
		err = obj.LName;
	} else if (obj.Phone.value == "") {
		err = obj.Phone;
	} else if (obj.Email.value == "") {
		err = obj.Email;
	} else {
		obj.submit();
	}
		alert("Please fill in all fields.");
		eval(err).focus();
}
//-->
</script>
I used this for a submit button"
Code:
<input type="button" value="Submit" onClick="verify()">
 
Thanks for the help guys....I took tviman's advice and searched with exact phrase instead of any words and found one that worked perfect. It was very similiar to Supra, but I used the onsubmit in the form tag.

Thanks again for all the help.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top