I need to come up with a way to enforce a pattern that I cannot enforce through the normal pattern specification.
I have a field that needs to be two numerical characters. If only one numerical character is entered, then it needs to make it two characters, with the first leading character being zero. So, if 3 is entered, then the field becomes 03.
Also, no non-numeric characters can be accepted. Unfortunately, if I try to specify a pattern in a numeric field, then it converts any non-numeric characters to zeros! That won't work for my application.
The plan I've come up with is the following: Make the field so that it is only wide enough for fixed-width characters to fit, and check the box to limit length of field to visible area. Then, enter code on the exit event that will check the characters that were entered one at a time, first the left and then the right. It can examine the ASCII values for those characters to make sure that they represent the 0-9 digits; that would mean that the ASCII value for each character has to be greater than or equal to 48 and less than or equal to 57. If either of the characters does not pass that test, then the computer beeps (like it does when I try to enter an alpha character into a numeric or decimal field, if it is possible to make it beep like that) and the field is cleared.
Can anyone help me with that code? I know I have to use javascript instead of formcalc (since there is no ASCII function in formcalc that I know of), and I don't know javascript, so I'm at a loss.
Thanks for any assistance...
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
As with most code, there are a number of ways to do this. Here is one example that you can add to the exit event of the field in question and make sure the language is set to JavaScript or else the code will generate an error. I have added comments for you to see what's happening:
var myRegex = /^\d{2}$/; // this matches a 2 digit pattern
var singleDigits = [0,1,2,3,4,5,6,7,8,9]; // array for single digit check
for (var i = 0; i < singleDigits.length; i++){ // loop through single digit array
if (this.rawValue == i){ // if value matches a digit in the array
this.rawValue = '0' + i; // add a '0' in front
}
}
if (this.rawValue != null || this.rawValue != ''){ // only if the field has a value...
if (!this.rawValue.match(myRegex)){ // and it doesnt match the pattern (i.e. 2 digits)
this.rawValue = ''; // clear the field
xfa.host.setFocus(this); // set focus back to the field
xfa.host.messageBox('Error: Pattern Conflict. Only 2 digits may be entered into this field.'); // enter any message you wish here
}
}
The field needs to be a text field for the '0' to be added in front, else if its a numeric field, the code will try to mathematically add the '0' to the number entered and you will be left with a single digit again.
Another option is to make the field a comb of 2 digits and then you can remove the second code block.
-
Dallas
Views
Replies
Total Likes
I assume that textfield takes input as alpha&numeric.
please check the attached sample.
Find the java script in variables as js and exit event of first textfield.
Hope this will help.
RAGHU.
Views
Replies
Total Likes
Hi,
As with most code, there are a number of ways to do this. Here is one example that you can add to the exit event of the field in question and make sure the language is set to JavaScript or else the code will generate an error. I have added comments for you to see what's happening:
var myRegex = /^\d{2}$/; // this matches a 2 digit pattern
var singleDigits = [0,1,2,3,4,5,6,7,8,9]; // array for single digit check
for (var i = 0; i < singleDigits.length; i++){ // loop through single digit array
if (this.rawValue == i){ // if value matches a digit in the array
this.rawValue = '0' + i; // add a '0' in front
}
}
if (this.rawValue != null || this.rawValue != ''){ // only if the field has a value...
if (!this.rawValue.match(myRegex)){ // and it doesnt match the pattern (i.e. 2 digits)
this.rawValue = ''; // clear the field
xfa.host.setFocus(this); // set focus back to the field
xfa.host.messageBox('Error: Pattern Conflict. Only 2 digits may be entered into this field.'); // enter any message you wish here
}
}
The field needs to be a text field for the '0' to be added in front, else if its a numeric field, the code will try to mathematically add the '0' to the number entered and you will be left with a single digit again.
Another option is to make the field a comb of 2 digits and then you can remove the second code block.
-
Dallas
Views
Replies
Total Likes
Most helpful once again. Thanks Dallas.
Again, for those of you as slow as I am, I had one thing that might save you some time... I kept getting an error on the "for" statement. I figured out that I had to put hard returns after the ";" symbols, so that it read as follows:
for (var i = 0;
i < singleDigits.length;
i++){ // loop through single digit array
I notice this editor is manipulating my syntax as I type though, so that code might not look the same when it posts. Just make sure you put hard returns after all of the semicolons within the parentheses of the statement.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies