Expand my Community achievements bar.

validation pattern for text field (number only)

Avatar

Former Community Member
Hi, Im trying to create a field that users can only enter number. If they enter text, an error message should pop up. I can't seem to get it to work although i got the error message to show when users enter text. However, im not sure what i need to put in the "validation pattern" box. Should it be 9 or 99999 or 999999999. None of those worked, as i have tried. If someone has any idea. Please help. Thanks.
13 Replies

Avatar

Former Community Member
Hi Jaclyn,



You have two options:



1. Create a numeric field (not text field)which will only except numeric values.



2. Create a text field. Go to Validation pattern (Object>Value). Use drop down and select 99999. Then select Validation Pattern Message error. Don't forget to enter your message.



Regards,



Chris

Avatar

Former Community Member
Chris,

That is exactly that i tried but it didn't work. 2. Create a text field. Go to Validation pattern (Object>Value). Use drop down and select 99999. Then select Validation Pattern Message error.

The error message pops up even when i enter number in the field, unless i enter exactly 5 numbers... (for 99999, i assume)... is there a work around for this so i still can enter as many numbers as i like?

Thanks.

Avatar

Former Community Member
Hi Jaclyn,



You can use a Regex to validate the field. You execute the valation script in the blur event of the field.



Place these in our doc level:



var onlyNum = /^\d*$/;

var onlyNumMsg = "only numeric values allowed";



function validateField (getFieldObject, getRegex, getAlertMsg, getFieldValue)

{

var fieldValue;

if (getFieldValue != null) fieldValue = getFieldValue;

else fieldValue = getFieldObject.value;



if (fieldValue != "" && getRegex.test(fieldValue) == false)

{

app.alert(getAlertMsg, 1, 0, "Jaclyn Tran");

getFieldObject.value = "";

getFieldObject.setFocus();



}

}



Put this script in the blur event of your text field you need to validate:



validateField (event.target, onlyNum, onlyNumMsg);



Trust this helps,



Chris Fourie

www.intelliform.co.za

Avatar

Former Community Member
Hi Jaclyn,



Sorry I gave you the Acrobat scripts and not Designer.



here are the Designer scripts:



function validateField (getField, getRegex, getAlertMsg)

{

var fieldFocus = xfa.resolveNode(getField);

var regex = new RegExp();

regex.compile(getRegex);

var runRegex = regex.test(fieldFocus.rawValue);



if (fieldFocus.rawValue != null && runRegex == false)

{

app.alert(getAlertMsg, 1, 0, "Your Caption");

fieldFocus.rawValue = null;

xfa.host.setFocus(getField);

}

}



Put this in the exit event of the text field:



docLevel.validateField (this.name, docLevel.onlyNum, docLevel.onlyNumMsg);



Regards,



Chris Fourie

www.inteliform.co.za

Avatar

Former Community Member
Hi Chris,

I put the script that you gave me in the validate & exit events of the text field. Under Object>Value of the text field, do i still need to put in 99999 for the validation Pattern box? and do i need to check the error box as well?

Right now, i got the error message "TextField1 validate failed. Did i not put the code in the right placE?

Please advise.

Thanks

Avatar

Former Community Member
Hi Jaclyn,



Remove everything from the validation Pattern box & uncheck the error box.



You just need to use the function "function validateField" as a doc level and "docLevel.validateField (this.name, docLevel.onlyNum, docLevel.onlyNumMsg); " you place in the exit event of the text field.



Regards,



Chris Fourie

www.intelliform.co.za

Avatar

Former Community Member
Hi Chris,

This is what i have in the events with Scripts:

----- form1.#subform[0].TextField1::exit - (JavaScript, client) ------------------------------------



docLevel.validateField (this.name, docLevel.onlyNum, docLevel.onlyNumMsg);



----- form1.#subform[0].TextField1::validate - (JavaScript, client) --------------------------------



function validateField (getField, getRegex, getAlertMsg)

{

var fieldFocus = xfa.resolveNode(getField);

var regex = new RegExp();

regex.compile(getRegex);

var runRegex = regex.test(fieldFocus.rawValue);



if (fieldFocus.rawValue != null && runRegex == false)

{

app.alert(getAlertMsg, 1, 0, "please enter only number");

fieldFocus.rawValue = null;

xfa.host.setFocus(getField);

}

}

I also took everything out in the Object>Value>Validation Pattern.

Now, when i enter text or number, the field takes both and throws no error message. I still want the error message if user enters text instead of number. I think im still missing something. Do you have a sample that you could send me? Thanks.

Jaclyn

Avatar

Level 7
You have to define the "onlyNum" RegExp and the "onlyNumMsg" variables and the "validateField" function as a document level script:



var onlyNum = /^\d*$/;

var onlyNumMsg = "Please enter only numeric values.";



function validateField (getField, getRegex, getAlertMsg)

{

var fieldFocus = xfa.resolveNode(getField);

var regex = new RegExp();

regex.compile(getRegex);

var runRegex = regex.test(fieldFocus.rawValue);



if (fieldFocus.rawValue != null && runRegex == false)

{

app.alert(getAlertMsg, 1, 0, "Your Caption");

fieldFocus.rawValue = null;

xfa.host.setFocus(getField);

}

}



And use the following function call for the "validation" action for the field in question:



docLevel.validateField (this.name, docLevel.onlyNum, docLevel.onlyNumMsg);

Avatar

Former Community Member
Geo,

I added the defining for "onlyNum" and "onlyNumMsg" but it's still not showing the error message. You and Chris keep saying "adding as the doc level" What does that mean? Did i not put the script in the right place?

Thanks.

Avatar

Level 7
Download "LiveCycle Designer Scripting Basics", http://www.adobe.com/devnet/livecycle/articles/lc_designer_scripting_basics/lc_designer_scripting_ba... , and read section 8 Creating and Reusing JavaScript Functions. For the provided code you will be creating a script object titled "docLevel" at the form level.

Avatar

Former Community Member
That works... Thanks much Geo and Chris.

:-)

Avatar

Former Community Member
How to Disable Print option from Toolbar in Pdf. plz reply.

Avatar

Former Community Member
How to Disable Print option in Pdf. i am using Adobe Designer Live Cycle. plz Reply