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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation Checks

Status
Not open for further replies.

Sunil606

Programmer
Dec 8, 2003
27
GB

I have a Registration form below, and I want to make sure that the users input data into all the fields before pressing submit. If a user does not enter anything, then I want a message box saying ‘You have not completed all fields’. They will only be able to submit their details once they have filled out all the fields. (The field Address2 can be left empty) How would I do this?

Below is the code that I am using for my registration form.

Thanks in advance

<html>
<head>

<SCRIPT language=&quot;JavaScript&quot;>
<!--
function VerifyData()
{
if (document.frmUser.txtPassword.value != document.frmUser.txtVerifyPassword.value)
{
alert (&quot;Your passwords do not match - please reenter&quot;);
return false;
}
else
return true;
}
-->
</SCRIPT>

<title>Registration Form</title>
</head>
<body>

<input type=&quot;button&quot; onClick=&quot;document.all.content.style.zoom=(document.all.content.style.zoom==1?2:1);&quot; value=&quot;ENLARGE TEXT&quot;>
<div id=&quot;content&quot;>

<H1 align=&quot;center&quot;><font face=&quot;Arial&quot;><u>Registration Form</u></font></H1>

<form action = &quot;adduser1.asp&quot; name=&quot;frmUser&quot; method = &quot;post&quot;
onSubmit=&quot;return VerifyData()&quot;>

Title <input type=&quot;text&quot; name = &quot;txtTitle&quot;><br>
First Name <input type=&quot;text&quot; name = &quot;txtFirstName&quot;><br>
Surname <input type=&quot;text&quot; name = &quot;txtSurname&quot;><br>
Address1 <input type=&quot;text&quot; name = &quot;txtAddress1&quot;><br>
Address2 <input type=&quot;text&quot; name = &quot;txtAddress2”><BR>
Town <input type=&quot;text&quot; name = &quot;txtTown&quot;><br>
County <input type=&quot;text&quot; name = &quot;txtCounty&quot;><br>
Post Code <input type=&quot;text&quot; name = &quot;txtPostCode&quot;><br>
Telephone Number <input type=&quot;text&quot; name = &quot;txtTelephoneNo&quot;><br>
Email Address <input type=&quot;text&quot; name = &quot;txtEmailAddress&quot;><br>
Username <input type=&quot;text&quot; name = &quot;txtUsername&quot;><br>
Password <input type=&quot;password&quot; name = &quot;txtPassword&quot;><br>
Verify Password <input type=&quot;password&quot; name = &quot;txtVerifyPassword&quot;><br>

<INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Submit&quot;>&nbsp;
<INPUT TYPE=&quot;RESET&quot; VALUE=&quot;Clear&quot;></font></P>
</form>
<p><font face=&quot;Arial&quot;><a href=&quot;home.asp&quot;>HOME</a></font></p>

</div>
</body>
</html>



 
thanks for that, but where in the form would i place it?
 
sunil606,

I've had a bit of a play with your code... try this:

Code:
<html>
<head>
<SCRIPT language=&quot;JavaScript&quot;>
<!--
function VerifyFields() {
	var msg=&quot;The following fields must be filled:&quot;;
	var blank=false;
	for(var x=0;x<document.forms[0].length;x++) {
		theObj=document.forms[0].elements[x];
		theType=theObj.type;
		theName=theObj.name;
		theValue=theObj.value;
		if((theType!=&quot;SUBMIT&quot;)&&(theType!=&quot;RESET&quot;)) {
			// an input field requiring validation - ignore txtAddress2
			if((theName!=&quot;txtAddress2&quot;)&&(theValue==&quot;&quot;)) {
				msg+=&quot;\n* &quot;+theName.substring(3,theName.length);
				blank=true;
			}
		}
	}
	if(blank) {
		alert(msg);
		return false;
	}
	return true;
}
function VerifyData() {
	if (VerifyFields()) {
		if (document.frmUser.txtPassword.value != document.frmUser.txtVerifyPassword.value) {
			alert (&quot;Your passwords do not match - please reenter&quot;);
			return false;
		} else return true;
	} else return false;
}
//-->
</SCRIPT>
<title>Registration Form</title>
</head>
<body style=&quot;font-family: arial&quot;>
<Button onClick=&quot;document.all.content.style.zoom=(document.all.content.style.zoom==2?1:2);&quot;>Grow/Shrink Text</button>
<div id=&quot;content&quot;>
<H1 align=&quot;center&quot;><u>Registration Form</u></H1>
<form action=&quot;adduser1.asp&quot; name=&quot;frmUser&quot; method = &quot;post&quot; onSubmit=&quot;return VerifyData();&quot;>
	Title: <input type=&quot;text&quot; name=&quot;txtTitle&quot;><br>
	First Name: <input type=&quot;text&quot; name=&quot;txtFirstName&quot;><br>
	Surname: <input type=&quot;text&quot; name= &quot;txtSurname&quot;><br>
	Address1: <input type=&quot;text&quot; name=&quot;txtAddress1&quot;><br>
	Address2: <input type=&quot;text&quot; name=&quot;txtAddress2&quot;><br>
	Town: <input type=&quot;text&quot; name=&quot;txtTown&quot;><br>
	County: <input type=&quot;text&quot; name=&quot;txtCounty&quot;><br>
	Post Code: <input type=&quot;text&quot; name=&quot;txtPostCode&quot;><br>
	Telephone Number: <input type=&quot;text&quot; name=&quot;txtTelephoneNo&quot;><br>
	Email Address: <input type=&quot;text&quot; name=&quot;txtEmailAddress&quot;><br>
	Username: <input type=&quot;text&quot; name=&quot;txtUsername&quot;><br>
	Password: <input type=&quot;password&quot; name=&quot;txtPassword&quot;><br>
	Verify Password: <input type=&quot;password&quot; name=&quot;txtVerifyPassword&quot;><br>
	<INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Submit&quot;>  <INPUT TYPE=&quot;RESET&quot; VALUE=&quot;Clear&quot;>
</form>
<p>
<a href=&quot;home.asp&quot;>HOME</a>
</div>
</body>
</html>

When you check if the Name is txtAddress2, you might want to add further validation for the email address (txtEmailAddress) - at the moment it simply checks for an empty string. :)

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
thanks, let me test that out and get back to you. :)
 
WarTookMan, thanks for that, it works a treat, how can i put a email validation check so that it has to be an email address not any old crap.
 
Try the following working solution:

Code:
<html>
<head>
<SCRIPT language=&quot;JavaScript&quot;>
<!--
function formValidateEmail(email) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(email);
}
function VerifyFields() {
	var msg=&quot;The following fields appear to be invalid:&quot;;
	var blank=false;
	for(var x=0;x<document.forms[0].length;x++) {
		theObj=document.forms[0].elements[x];
		theType=theObj.type;
		theName=theObj.name;
		theValue=theObj.value;
		if((theType!=&quot;SUBMIT&quot;)&&(theType!=&quot;RESET&quot;)) {
			// check if the field is the Email address and validate
			if((theName==&quot;txtEmailAddress&quot;)&&(!formValidateEmail(theValue))) {
				msg+=&quot;\n* &quot;+theName.substring(3,theName.length);
				blank=true;
			}
			// check the other fields but ignore txtAddress2
			if((theName!=&quot;txtAddress2&quot;)&&(theValue==&quot;&quot;)) {
				msg+=&quot;\n* &quot;+theName.substring(3,theName.length);
				blank=true;
			}
		}
	}
	if(blank) {
		alert(msg);
		return false;
	}
	return true;
}
function VerifyData() {
	if (VerifyFields()) {
		if (document.frmUser.txtPassword.value != document.frmUser.txtVerifyPassword.value) {
			alert (&quot;Your passwords do not match - please reenter&quot;);
			return false;
		} else return true;
	} else return false;
}
//-->
</SCRIPT>
<title>Registration Form</title>
</head>
<body style=&quot;font-family: arial&quot;>
<Button onClick=&quot;document.all.content.style.zoom=(document.all.content.style.zoom==2?1:2);&quot;>Grow/Shrink Text</button>
<div id=&quot;content&quot;>
<H1 align=&quot;center&quot;><u>Registration Form</u></H1>
<form action=&quot;adduser1.asp&quot; name=&quot;frmUser&quot; method = &quot;post&quot; onSubmit=&quot;return VerifyData();&quot;>
	Title: <input type=&quot;text&quot; name=&quot;txtTitle&quot;><br>
	First Name: <input type=&quot;text&quot; name=&quot;txtFirstName&quot;><br>
	Surname: <input type=&quot;text&quot; name= &quot;txtSurname&quot;><br>
	Address1: <input type=&quot;text&quot; name=&quot;txtAddress1&quot;><br>
	Address2: <input type=&quot;text&quot; name=&quot;txtAddress2&quot;><br>
	Town: <input type=&quot;text&quot; name=&quot;txtTown&quot;><br>
	County: <input type=&quot;text&quot; name=&quot;txtCounty&quot;><br>
	Post Code: <input type=&quot;text&quot; name=&quot;txtPostCode&quot;><br>
	Telephone Number: <input type=&quot;text&quot; name=&quot;txtTelephoneNo&quot;><br>
	Email Address: <input type=&quot;text&quot; name=&quot;txtEmailAddress&quot;><br>
	Username: <input type=&quot;text&quot; name=&quot;txtUsername&quot;><br>
	Password: <input type=&quot;password&quot; name=&quot;txtPassword&quot;><br>
	Verify Password: <input type=&quot;password&quot; name=&quot;txtVerifyPassword&quot;><br>
	<INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Submit&quot;>  <INPUT TYPE=&quot;RESET&quot; VALUE=&quot;Clear&quot;>
</form>
<p>
<a href=&quot;home.asp&quot;>HOME</a>
</div>
</body>
</html>

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
thanks very much for that, it works a beauty :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top