Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | ColdFusion | Q3003 [ previous next ]

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

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.