Expand my Community achievements bar.

Where to put the validation code?

Avatar

Former Community Member
I have a numeric field, where user enters SSN in the format 999-99-9999.

In the validate event of the numeric field I wrote



if(SSN.format!="999-99-9999"){

app.alert("Please enter a valid SSN in format 999-99-9999");

}

It displays this message when user tabs out of the field, but even if the format is correct, it still shows this message. Where should I put this script in???
12 Replies

Avatar

Former Community Member
If you set the display pattern, validation pattern and validation pattern message for the field, you can get this popup without having to write any script at all - drop in the "U.S. Social Security Number" object from the Custom tab in the library to see an example of an SSN field that pops up a message when you enter an invalid format.



SSN.format isn't the formatting pattern for the field, it's actually some other object, so comparing it with a validation pattern will always fail (which is why you're always seeing the message).

Avatar

Former Community Member
Thanks...but how can I make the focus to be set back on the SSN field after the message is displayed??

Avatar

Former Community Member
Is it possible that I use numeric fields for SSN and Zip Code and if the user doesnt put in the specified format, an error message is displayed. I did the same way for text field..it works but not for numeric fields...why???

Avatar

Former Community Member
If you want focus to return to the SSN field if the user tabs out or clicks away without filling it in (or enters an invalid format), you'll need to write script.



Acrobat's default behaviour is not to prevent the user from tabbing from one field to the next even if a field's value is marked as "required". Acrobat will, however, prevent the form from being submitted if a field is marked as required and isn't filled.



Acrobat will also prevent a form from being submitted if a field's validation failed and this validation was marked as an "error" instead of a warning.



You have a few options:



1. On the SSN custom object you placed on your form, go into the Object palette's Value tab and set the field's value
Type to
User Entered - Required and set the
Empty Message box that appears to an error message to be displayed if the field is left empty. Upon attempting to submit the form (via email, HTTP, etc.), Acrobat will identify the required field and require that it be filled.



2. Check the
Error check box above the
Validation Pattern Message box on the Object palette's Value tab. This specifies that a validation pattern failure should be treated as an error and will prevent the form from being submitted until the field contains a valid SSN.



3. Write a script, in the SSN object's
Exit event that checks to see if the value of the object is empty (or null if the user has yet to type something in it and is just tabbing over the object) and warns of the empty value and sets the focus back to the SSN object:






if (SSN.rawValue == null || SSN.rawValue == "")

{

app.alert("SSN is required!");

xfa.host.setFocus("SSN");

}






Options 1 and 2 are good if you don't need to verify until form submit time. Option 3 is good if you want to verify immediately.

Avatar

Former Community Member
Thanks so much..My form needs to be printed and not submitted. I have chosen a numeric field to be the SSN...so that user cannot enter alphabets. If he does, Can I display an error message that says enter only numeric value. Also, if the user enters less that 9 numbers can I prompt to enter 9 numbers??



If i write a message in validation pattern message in the object palette..it displays only if user enters more tha 9 numbers...not in case of alphabets or less than nine. I have used 999-99-9999 as display pattern as well as validation pattern.



Thanks

Marleen

Avatar

Former Community Member
In response to your problems with using Numeric Fields for the SSN and Zip Code data, the problem is that values entered into Numeric Fields are always evaluated to numbers (in the underlying data) as opposed to text (as with the Text Field objects, like the SSN custom object).



In a Numeric Field, setting a
Display pattern of "999-99-9999" and entering a value of "123" will result in a value of 123 for the field and Acrobat will display it as "000-00-0123" because the 9s in the pattern specify that the number must be displayed and Acrobat will display zeros for the ones that don't pertain to an actual digit in the value.



It's different with Text Fields (like the default object type of the SSN custom object) because what's entered is interpreted as a string and matched against the pattern. In this case, Acrobat won't replace the missing "digits" with a default character so the validation error arises...

Avatar

Former Community Member
Thank you so much for your response....now its getting clear :)



So can I do something like, make SSN a text field and give the error message in script that only numbers should be entered rather than in object palette defining the validation pattern as 999999999...because I want the focus to go back onto the SSN field after the message is displayed.



SSN.format doesnt work..as Steve Tibbett said..its not for the format to be entered/displayed...what should I choose??

Avatar

Former Community Member
Unfortunately you need to write the validation code yourself to do the same work that Acrobat is doing when it's validating... so in the SSN.exit event you'd put in some script like this:








var ssn = SSN.rawValue;

var ssnOk = 1;



// validate length

if (ssn.length != 11)

ssnOk = 0;



// Insert more validations (dashes in right place, all digits)



// finally.. show a message if validation failed

if (ssnOk == 0)

{

app.alert("Please enter a valid SSN in format 999-99-9999");

xfa.host.setFocus("SSN");

}





Avatar

Former Community Member
Thanks..this works great!!



But I am still having difficulty in checking if the user entered only numbers and no alphabets or special characters...should I use the ascii values to perform the check??

Avatar

Former Community Member
You could do that but I think it would be simpler to extract the numerical portions of the value entered into the SSN field and evaluate if they're numbers using the JavaScript Number object.



Following is a script that assumes the value entered into the SSN field as the SSN number is always entered as "999999999":






var nValue = new Number(SSN.rawValue);

app.alert(isNaN(nValue) ? "failure" : "success");






This script will show a message box with "success" as the message if the value entered into the SSN field evaluates to a number and "failure" if it doesn't.



You'll probably need to add a little to this script in order to handle the different ways in which the user may enter the SSN number. For instance, you should probably test for the "999-99-9999" by using the
indexOf method on SSN.rawValue (a string). If SSN.rawValue.indexOf("-") returns 0 or more, then you know the position of a hyphen in the value and you can test the characters up to, but not including the hyphen as being a number or not (SSN.rawValue.substring(0, hyphenPos))...

Avatar

Former Community Member
Yeah.. here's a more complete example:<br /><br />var ssn = SSN.rawValue;<br><br />var ssnOk = 1;<br><br /><br><br />// validate length<br><br />if (ssn.length != 11)<br><br /> ssnOk = 0;<br><br /><br><br />for (var i = 0; i<ssn.length; i++)<br><br />{<br><br /> if (i == 3 || i == 6)<br><br><br /> {<br><br /> // check for dashes in the right place<br><br /> if (ssn[i] != '-')<br><br /> ssnOk = 0;<br><br /> } else<br><br /> {<br><br /> // check that everything else is a number<br><br /> if (ssn[i] < '0' || ssn[i] > '9')<br><br /> ssnOk = 0;<br><br /> }<br><br />}<br><br /><br><br />// finally.. show a message if validation failed <br><br />if (ssnOk == 0)<br><br />{<br><br /> app.alert("Please enter a valid SSN in format 999-99-9999");<br><br /> xfa.host.setFocus("SSN");<br><br />}<br><br /></pre><br /><br />--<br />Steve Tibbett<br />Adobe Systems

Avatar

Former Community Member
Wow..this works super great..much much simpler than what i was planning to do...i guess I need to understand how the script is read when executed.



Thanks so much both of you...