Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Limitation of characters

Avatar

Former Community Member
Hi,



I need to limit a textfield to the following characters "0-9, {}, (), +,-, /, . and space allowed" as well as another textfield to "0-9 a-z A-Z & .spaces allowed", but I have no idea how.



Can someone please help me?



Thanks in advance,

Claudi
5 Replies

Avatar

Former Community Member
if (str.indexOf("0")!= -1 || str.indexOf("9") != -1 || str.indexOf("+")!= -1 )



{

return true;

}



else if (str.indexOf("*")!= -1 ) <-means this must be 0 or wun exist



{

return false;

xfa.host.messageBox("this is not allowed!);

}



each str.indexOf("something")!=-1 if return True, means that the character being pointed must be included in the string and exist. hence it will NOT be a Negative number. I hope it helps and work for you.



\ and "" doesn't work for this method.

For space, use str.indexOf(" ")!= -1



Return false is to block those characters that you don't allow.

Avatar

Former Community Member
Hi!<br /><br /> I've managed to figure out a better version since I also need it.<br /><br />var field = TextField.rawValue;<br />var a = 'abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+0123456789/" "';<br /><br />var count=0;<br /><br />for (var i=0; i<field.length; i++){<br />if (a.indexOf(field.charAt(i))== "-1")<br />{<br />count += 1;<br />}<br />}<br /><br />if (count>0) <br />{<br />xfa.host.messageBox("Invalid characters!");<br />xfa.host.setFocus("xfa.form.PurchaseOrderConfirmation.PurchaseOrderConfirmation.ContactInfo.TextField");<br />}<br /><br />Var a is to store those characters you want to allow. count is to store the number of results that is not an 'allowed char'. Hope it helps! Copy it into your program to view better.=)

Avatar

Former Community Member
Hi tyy,

it works really fine!



Thanks a lot,

Claudi

Avatar

Former Community Member
Easy.

In the first fields exit event use:






var strTest = this.rawValue;

var strValid = (strTest != null)? strTest.replace(/[^\d\{\}\(\)\+\-\/\.\s]*/g, "") : null;

//--- remove the following 3 lines, if you do not want to display an error message

if (strValid != strTest){

xfa.host.messageBox("Characters allowed: 0-9{}()+-. and whitespace.", "Illegal character entered");

}

//---

this.rawValue = strValid;




and in the other one:






var strTest = this.rawValue;

var strValid = (strTest != null)? strTest.replace(/[^A-Za-z\d\s\&\.]*/g, "") : null;

//--- remove the following 3 lines, if you do not want to display an error message

if (strValid != strTest){

xfa.host.messageBox("Characters allowed: A-Za-z0-9&. and whitespace.", "Illegal character entered");

}

//---

this.rawValue = strValid;






Steve

Avatar

Former Community Member
May I know how to validate date and time?



Eg.

21/04/2007 10:34:03 AM

dd/mm/yyyy hh:mm:ss