Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Script for validating Text fields or numeric fields

Avatar

Former Community Member
I'm looking for a simple script that will check the entered data in a specific field.If i enter numbers in the name/last name field i should get a pop up message that entering numbers in this field are not allowed.

(And vice versa regarding numeric fields).

What is the script for that ?
4 Replies

Avatar

Former Community Member
Hi,



try this:






//var reg = /^\d+$/;    // checks whether input contains ONLY digits



var reg = /\d+/;    // checks whether input contains digits AND letters







var check = reg.exec(xfa.resolveNode("#subform[0].#field[0]").rawValue);



if (check == null)



    app.alert("Everything's fine!");



else



    app.alert("Entering digits is not allowed!");





You may want to replace
#subform[0] with the name of the subform the field to check is located in and
#field[0] with the name of the field to check for digits.



Also, you may want to select any of the regular expressions, which fits best your needs. See comment in the above code-fragement for further information.



Regards,

Steve

Avatar

Former Community Member
Hi steve ,

Thanks for the help.



I'm not sure but it seems that the following line :

var reg = /^\d+$/; checks whether input contains ONLY characters and not digits, how do i change it so it will check whether input contains only digits ?



In addition , after i see the message and press OK values still remains in the field.What should i add to the code so the values will be reset.



Thanks

Avatar

Former Community Member
Actually,
/^\d+$/ checks for one or more digits only.



To reset the fields value, you may use anything like




xfa.resolveNode("#subform[0].#field[0]").rawValue = "";





Regards,

Steve

Avatar

Former Community Member
Thanks for the code...it worked great for me too.



Ken