Hi everybody.
I have a form with a text field provided with a validation script, which allows the user only to enter values in the format 999999-9999. The script is places on the exit event.
var myRegex = /\d{6}\-\d{4}/;
var
singleDigits = [0,1,2,3,4,5,6,7,8,9];
If the format is not correct, the user will get an error-message.
Now I would like the hyphen to appear automatically when typing. That means when the user has typed 123456 it will display as 123456-, ready to enter the last four characters.
Is that possible.
Kirstine
P.S. I know I can make that (almost) happen if I make the field a numeric field, but for several reasons this is not an option.
Solved! Go to Solution.
Views
Replies
Total Likes
Here is a little script that can do what you are looking for..
Place the script in the Change event of the Text Field and set the Max chars property to 11 for the Text Field.
// restrict entry to digits and a dash
if (xfa.event.change.match(/[0-9\-]/) == null)
xfa.event.change = "";
// Allow the hyphen at the 7th character only
if (xfa.event.change == "-" && xfa.event.selEnd != 6)
xfa.event.change = "";
// If the 7th character is a digit, and they're typing at the end, insert the hyphen
if (xfa.event.change.match(/[0-9]/) != null &&
xfa.event.newText.length == 7 &&
xfa.event.selEnd == 6)
{
xfa.event.change = "-" + xfa.event.change;
}
// don't allow any characters past 10 (11 with a hyphen)
var vMax = 10;
if (xfa.event.newText.indexOf("-") != -1)
vMax = 11;
Thanks
Srini
Views
Replies
Total Likes
HI Kristine,
It is not exactly what you want but you could try a display pattern on your text field of "text{999999-9999}". This would put the hyphen in but only when the user exits the field and only if the value entered matches the display pattern (in your case ten digits). One advantage of this is that you can validate the value in the exit event with code something like;
if (this.formattedValue == this.rawValue)
{
app.alert("error");
}
I have coded something similar to what you want but it did get quiet complicated when the user deleted characters, backspaced over the hyphen or cut-and-pasted something but the user experience wasn't that great, they didn't seem to like things happening that they didn't expect. You maybe better off with two fields.
Anyway the pattern can be entered by clicking the "Pattern" button on the field tab of the Object palette.
Good luck,
Bruce
Views
Replies
Total Likes
Here is a little script that can do what you are looking for..
Place the script in the Change event of the Text Field and set the Max chars property to 11 for the Text Field.
// restrict entry to digits and a dash
if (xfa.event.change.match(/[0-9\-]/) == null)
xfa.event.change = "";
// Allow the hyphen at the 7th character only
if (xfa.event.change == "-" && xfa.event.selEnd != 6)
xfa.event.change = "";
// If the 7th character is a digit, and they're typing at the end, insert the hyphen
if (xfa.event.change.match(/[0-9]/) != null &&
xfa.event.newText.length == 7 &&
xfa.event.selEnd == 6)
{
xfa.event.change = "-" + xfa.event.change;
}
// don't allow any characters past 10 (11 with a hyphen)
var vMax = 10;
if (xfa.event.newText.indexOf("-") != -1)
vMax = 11;
Thanks
Srini
Views
Replies
Total Likes
Thanks Srini, so much.
I really appreciate all the help I get in this forum.
Kirstine
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies