JSF SSN client-side validator demo
How to get this to work in your own pageThis demo does not require you to enter an SSN, if you do not enter an SSN it will return to the same page without giving an error. However if you do enter some values for the SSN it will call the client-side validator to validate the SSN before sending it off to the server. The input field here requires you to enter the SSN without any dashes, because we are not using the SSNConverter companion class.
<h:outputScript library="com.manorrock.jsf.ssn" name="SSNValidator.js" target="head"/>
<script type="text/javascript">
function validate(element) {
var ssn = element.value;
if (!(ssn == "")) {
if (!manorrock.jsf.ssn.SSNValidator.validate(ssn)) {
alert('SSN is invalid');
return false;
}
}
}
</script>
<h:inputText value="#{ssnBean.ssn}" onchange="validate(this);">
</h:inputText>
Actual demo