JavaScript validation to format tax id field based on check box value.
Copy link to clipboard
Copied
I have 3 fields. TaxID (text field) isSSN (checkbox) isTIN (checkbox). I would like to update the formatting on the TaxID text field based on the check box selected. isSSN = format xxx-xx-xxx or isTIN = format xx-xxxxxx.
Copy link to clipboard
Copied
If you have a modern form and these fields are html5 you will have the pattern attribute available. http://html5pattern.com/
Essentially this is a regular expression validator for the input.
With Javascript you can then simply just update the attribute value with a new expression depending on the value of another field.
If you are not building your HTML in html5 then you would in your scripting have to values representing the validation of the field. On Blur of the field you can validate the content entered as well as format it depending on the option of the other field value.
Copy link to clipboard
Copied
One of the most important things in any multipurpose forum is to tell us what product you're using to calculate this. Only then can we help you.
In your brief example, the format of the resulting number seems to be the purpose of the script. It needs validation that one is even selected, which a radio would be better at (one literally must be selected by default). After that the JavaScript is usually fairly simple, but it depends entirely on what this is. A website, PDF, etc..
Copy link to clipboard
Copied
I have an Adobe form, please see attached. I have a Radio Button called SSNEIN for the user to select if they have a Social Security Number or an Employer Identification Number. Then I want them to enter the number, and automatically format it based on the selection (SSN or EIN). In Actions, On Blur, Run a JavaScript, I have:
f = getField(event.target.name)
if (f.value.length == 0)
{
f.setFocus()
app.alert("This field is required. Please enter the Social Security or Employer Identification Number of Primary Owner.")
}
In Validate, Run custom validation script, I have:
if (event.value)
{
if (/^\d{9}$/.test(event.value)==false && (/^\d{2}\-\d{7}$/.test(event.value)==false && (/^\d{3}\-\d{2}\-\d{4}$/.test(event.value)==false)))
{
app.alert("You entered an Invalid Tax ID Number. It must contain only numbers without dashes or numbers with the dashes in the correct position(s}.",1); event.rc = false;
}
}
// Format as Social Security Number or EIN
var Q = this.getField("SSNEIN");
if(Q == "SSN")
{
if (event.value)
{
event.value = util.printx("999-99-9999", event.value);
}
else
{
event.value = util.printx("99-9999999", event.value);
}
}
The On Blur works. Where it checks for a valid set of characters works. The last part does not work. It does nothing. No errors, just doesn't work. Can you tell me where I went wrong? Thank you very much.
Copy link to clipboard
Copied
To be completely honest, NONE of that code originated with me. I'm a complete N00B. I searched for and found the code I needed and adapted it for my purpose. While I understand nearly all of it, I'm clueless about the first part of the Validation. It works perfectly, but ... what does it mean? I would love someone to parse it and tell me what each little thing means/does. Thank you so much!
Copy link to clipboard
Copied
Post in the Acrobat SDK forum please. This SHOULD be a good forum for you, but it hasn't worked out that way.

