SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot sethtrain's Avatar
    Join Date
    Dec 2003
    Location
    Paducah, KY
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validating with regular expressions

    I have a form and validation function that is validating the information sent to it with regular expressions. Here is the form:

    HTML Code:
    <form action="" method="post" onsubmit="return Verify(this);">
    <table class="eighty">
    	<tr>
    		<td><label for="name">Name:</label></td>
    		<td><input type="text" name="name" id="name" /></td>
    	</tr>
    	<tr>
    		<td><label for="social">Social Security #:</label></td>
    		<td><input type="text" name="social" id="social" /></td>
    	</tr>
    	<tr>
    		<td><label for="school">School:</label></td>
    		<td><input type="text" name="school" id="school" /></td>
    	</tr>
    	<tr>
    		<td><label for="address">School Address:</label></td>
    		<td><input type="text" name="address" id="address" /></td>
    	</tr>
    	<tr>
    		<td><label for="city">School City:</label></td>
    		<td><input type="text" name="city" id="city" /></td>
    	</tr>
    	<tr>
    		<td><label for="state">School State:</label></td>
    		<td><input type="text" name="state" id="state" /></td>
    	</tr>
    	<tr>
    		<td><label for="zip">School Zip:</label></td>
    		<td><input type="text" name="zip" id="zip" /></td>
    	</tr>
    	<tr>
    		<td><label for="phone">School Phone:</label></td>
    		<td><input type="text" name="phone" id="phone" /></td>
    	</tr>
    	<tr>
    		<td><label for="email">School Email:</label></td>
    		<td><input type="text" name="email" id="email" /></td>
    	</tr>
    		<td><label for="haddress">Home Address:</label></td>
    		<td><input type="text" name="haddress" id="haddress" /></td>
    	</tr>
    	<tr>
    		<td><label for="hcity">Home City:</label></td>
    		<td><input type="text" name="hcity" id="hcity" /></td>
    	</tr>
    	<tr>
    		<td><label for="hstate">Home State:</label></td>
    		<td><input type="text" name="hstate" id="hstate" /></td>
    	</tr>
    	<tr>
    		<td><label for="hzip">Home Zip:</label></td>
    		<td><input type="text" name="hzip" id="hzip" /></td>
    	</tr>
    	<tr>
    		<td><label for="hphone">Home Phone:</label></td>
    		<td><input type="text" name="hphone" id="hphone" /></td>
    	</tr>
    	<tr>
    		<td><label for="hemail">Home Email:</label></td>
    		<td><input type="text" name="hemail" id="hemail" /></td>
    	</tr>
    	<tr>
    		<td><label for="ta0405">Teaching Assignment 2004-05:</label></td>
    		<td><input type="text" name="ta0405" id="ta0405" /></td>
    	</tr>
    	<tr>
    		<td><label for="ta0506">Teaching Assignment 2005-06:</label></td>
    		<td><input type="text" name="ta0506" id="ta0506" /></td>
    	</tr>
    	<tr>
    		<td><label for="highdegree">Highest Degree/Area:</label></td>
    		<td><input type="text" name="highdegree" id="highdegree" /></td>
    	</tr>
    	<tr>
    		<td><label for="certification">Certification:</label></td>
    		<td><input type="text" name="certification" id="certification" /></td>
    	</tr>
    	<tr>
    		<td><label for="yrsofexp">Years of Experience:</label></td>
    		<td><input type="text" name="yrsofexp" id="yrsofexp" /></td>
    	</tr>
    	<tr>
    		<td><label for="yrsinpos">Years in Current Position:</label></td>
    		<td><input type="text" name="yrsinpos" id="yrsinpos" /></td>
    	</tr>
    	<tr>
    		<td><label for="enhancement">How will Institute enhance student learning?</label></td>
    		<td><textarea name="enhancement" id="enhancement" rows="5" cols="20"></textarea></td>
    	</tr>
    	<tr>
    		<td><label for="otherattended">Other TQI Institutes attended/Year:</label></td>
    		<td><textarea name="otherattended" id="otherattended" rows="5" cols="20"></textarea></td>
    	</tr>
    	<tr>
    		<td>Course Credit:</td>
    		<td>
    			<cfif #attributes.id# is "SC1">
    			<label for="edu651">EDU651:</label> <input type="radio" name="coursecredit" id="edu651" value="edu651" checked="checked">
    			<label for="gsc693">GSC693:</label> <input type="radio" name="coursecredit" id="gsc693" value="gsc693">
    			<cfelse>
    				EDU651 <input type="hidden" name="coursecredit" value="edu651" />
    			</cfif>
    			<input type="hidden" name="institute" value="<cfoutput>#topic#</cfoutput>" /> <input type="hidden" name="date" value="<cfoutput>#DateFormat(now(), "mm/dd/yyyy")#</cfoutput>" />
    		</td>
    	</tr>
    	<tr>
    		<td><input type="submit" value="Register" /></td>
    	</tr>
    </table>
    </form>
    and here is the javascript code:
    Code:
    function Verify(x)
    	{
    		var noerrors = true;
    		var returnvalue;
    		var funAnswer;
    		var ck_Radio;
    		var reg_isNumber = /^[0-9]+$/;
    		var reg_zip = /^[0-9]{5}$/;
    		var reg_mail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    		var obj = document.forms[0];
    		var msg = "---------------------------------------------------\n";
    		msg += "    The following errors where discovered. \n";
    		msg += "    Please correct them before continuing. \n";
    		msg += "---------------------------------------------------\n";
    	
    		if (NameVerify(obj.name.value) == false)
    		{
    			noerrors = false;
    			msg += "- Name is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.social.value) == false)
    		{
    			noerrors = false;
    			msg += "- Social Security # not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.school.value) == false)
    		{
    			noerrors = false;
    			msg += "- School is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.address.value) == false)
    		{
    			noerrors = false;
    			msg += "- School Address is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.city.value) == false)
    		{
    			noerrors = false;
    			msg += "- School City is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.state.value) == false)
    		{
    			noerrors = false;
    			msg += "- School State is not filled out correctly.\n";
    		}
    		
    		if ((obj.zip.value.length < 5) || (reg_zip.test(obj.zip.value) == false))
    		{
    			noerrors = false;
    			msg += "- School Zip is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.phone.value) == false)
    		{
    			noerrors = false;
    			msg += "- Sochool Phone is not filled out correctly.\n";
    		}
    		
    		if (reg_mail.test(obj.email.value) == false)
    		{
    			noerrors = false;
    			msg += "- School Email is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.haddress.value) == false)
    		{
    			noerrors = false;
    			msg += "- Home Address is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.hcity.value) == false)
    		{
    			noerrors = false;
    			msg += "- Home City is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.hstate.value) == false)
    		{
    			noerrors = false;
    			msg += "- Home State is not filled out correctly.\n";
    		}
    		
    		if ((obj.hzip.value.length < 5) || (reg_zip.test(obj.hzip.value) == false))
    		{
    			noerrors = false;
    			msg += "- Home Zip is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.hphone.value) == false)
    		{
    			noerrors = false;
    			msg += "- Home Phone is not filled out correctly.\n";
    		}
    		
    		if (reg_mail.test(obj.hemail.value) == false)
    		{
    			noerrors = false;
    			msg += "- Home Email is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.ta0405.value) == false)
    		{
    			noerrors = false;
    			msg += "- Teacher Assignment for 04-05 is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.ta0506.value) == false)
    		{
    			noerrors = false;
    			msg += "- Teacher Assignment for 05-06 is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.highdegree.value) == false)
    		{
    			noerrors = false;
    			msg += "- Highest Degree/Area is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.certification.value) == false)
    		{
    			noerrors = false;
    			msg += "- Certification is not filled out correctly.\n";
    		}
    		
    		if (reg_isNumber.test(obj.yrsofexp.value) == false)
    		{
    			noerrors = false;
    			msg += "- Years of Experience is not filled out correctly.\n";
    		}
    		
    		if (reg_isNumber.test(obj.yrsinpos.value) == false)
    		{
    			noerrors = false;
    			msg += "- Years in Position is not filled out correctly.\n";
    		}
    		
    		if (NameVerify(obj.enhancement.value) == false)
    		{
    			noerrors = false;
    			msg += "- Student Learning Enhancement is not filled out correctly.\n";
    		}
    		
    		if (noerrors)
    		{
    			return true;
    		}
    		else
    		{
    			alert(msg);
    			return false;
    		}
    	}
    
    function NameVerify(passName)
    	{
    		var reName = /^[0-9a-zA-Z'-]+$/;
    		var rValue;
    		var GiveBack = true;
    		//alert("passName : " + passName);
    		if (passName.length < 1)
    		{
    			GiveBack = false;
    		}
    		else
    		{
    			rValue = reName.test(passName);
    			if (!rValue) 
    			{
    				GiveBack = false;
    			}
    		}
    		return GiveBack;
    	}
    I think the problem is in the NameVerify function but I am not sure.

    Some sample text would be:

    Name: Seth Buntin
    Social: 555-55-5555
    Etc...

    The form information is pretty self explainitory.
    --
    sethtrain

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've edited your code a bit, hope it makes more sense now

    Code:
    function Verify(form) {
    	var reg_name   = /^[0-9a-zA-Z'-]+$/;
    	var reg_number = /^[0-9]+$/;
    	var reg_zip    = /^[0-9]{5}$/;
    	var reg_mail   = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    	var msg = "";
    
    	if (!reg_name.test(form.name.value))
    		msg += "- Name is not filled out correctly.\n";
    	if (!reg_name.test(form.social.value))
    		msg += "- Social Security # not filled out correctly.\n";
    	if (!reg_name.test(form.school.value))
    		msg += "- School is not filled out correctly.\n";
    	if (!reg_name.test(form.address.value))
    		msg += "- School Address is not filled out correctly.\n";
    	if (!reg_name.test(form.city.value))
    		msg += "- School City is not filled out correctly.\n";
    	if (!reg_name.test(form.state.value))
    		msg += "- School State is not filled out correctly.\n";
    	if (!reg_zip.test(form.zip.value))
    		msg += "- School Zip is not filled out correctly.\n";
    	if (!reg_name.test(form.phone.value))
    		msg += "- Sochool Phone is not filled out correctly.\n";
    	if (!reg_mail.test(form.email.value))
    		msg += "- School Email is not filled out correctly.\n";
    	if (!reg_name.test(form.haddress.value))
    		msg += "- Home Address is not filled out correctly.\n";
    	if (!reg_name.test(form.hcity.value))
    		msg += "- Home City is not filled out correctly.\n";
    	if (!reg_name.test(form.hstate.value))
    		msg += "- Home State is not filled out correctly.\n";
    	if (!reg_zip.test(form.hzip.value))
    		msg += "- Home Zip is not filled out correctly.\n";
    	if (!reg_name.test(form.hphone.value))
    		msg += "- Home Phone is not filled out correctly.\n";
    	if (!reg_mail.test(form.hemail.value))
    		msg += "- Home Email is not filled out correctly.\n";
    	if (!reg_name.test(form.ta0405.value))
    		msg += "- Teacher Assignment for 04-05 is not filled out correctly.\n";
    	if (!reg_name.test(form.ta0506.value))
    		msg += "- Teacher Assignment for 05-06 is not filled out correctly.\n";
    	if (!reg_name.test(form.highdegree.value))
    		msg += "- Highest Degree/Area is not filled out correctly.\n";
    	if (!reg_name.test(form.certification.value))
    		msg += "- Certification is not filled out correctly.\n";
    	if (!reg_number.test(form.yrsofexp.value))
    		msg += "- Years of Experience is not filled out correctly.\n";
    	if (!reg_number.test(form.yrsinpos.value))
    		msg += "- Years in Position is not filled out correctly.\n";
    	if (!reg_name.test(form.enhancement.value))
    		msg += "- Student Learning Enhancement is not filled out correctly.\n";
    	
    	if (!msg.length)
    		return true;
    
    	msg = 
    		"---------------------------------------------------\n"
    		+ "    The following errors where discovered. \n"
    		+ "    Please correct them before continuing. \n"
    		+ "---------------------------------------------------\n"
    		+ msg;
    	alert(msg);
    	return false;
    }
    hth


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •