Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q3003 Is there a way to validate that a Social Security Number has been entered in a form field?

You are here: irt.org | FAQ | ColdFusion | Q3003 [ previous next ]

A: Yes, you can do it a couple of ways. If you don't want to do it with JavaScript, you can still do it with ColdFusion. The script below provides a sample submission form as well as the validation code.

<cfset valid = "true">
<cfif isDefined("formSubmit")>
	<cfloop from="1" to="11" index="count">
		<cfif (count is "4") or (count is "7")>
			<cfif Mid(SSN,count,1) neq "-">
				<cfset valid="false">
			</cfif>					
		<cfelse>
			<cfif REFind("[^0-9]",Mid(SSN,count,1),1) gt "0">
				<cfset valid="false">
			</cfif>
		</cfif>	
	</cfloop>
	<cfif valid is "false">
		A valid social security number was not submitted!
	<cfelse>
		A valid social security number was submitted!	
	</cfif>
<cfelse>
	<form action="test2.cfm" method="post">
		SSN (xxx-xx-xxxx):<br>
		<input name="SSN" type="Text" size="20" maxlength="11">
		<input type="Submit" name="formSubmit" value="Submit">
	</form>
</cfif>

The following was submitted by HERMIER Christophe

This question have already been answered but the answer was far too complicated !!! You can do it all with just one regular expression no need to loop !!

<CFIF ReFind ( SSN, "^[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ) is 0 >
   <CFOUTPUT>
   "#SSN#" is not a valid SSN !
   </CFOUTPUT>
<CFELSE>
   OK : valid SSN number
</CFIF>

Feedback on 'Q3003 Is there a way to validate that a Social Security Number has been entered in a form field?'

©2018 Martin Webb