How do I retain a 0 in the numeric formatted fields when the zero is at the beginning of a number? right now the field drops the 0 and proceeds to display the second number.
For example, in the number field, if i enter "00100", then it displays as "100".
I have written a keystroke script to accept only numbers but I need to limit the numbers to 6 digits
Please help.
function numOnly_ks() {
// Get all of the characters that have been entered in to the field
var value = AFMergeChange(event);
// Do nothing if field is blank
if(!value) return;
// Reject the entry if the entry doesn't match the regular expression
if(!AFExactMatch(/^[0-9 /+]+$/, value)) event.rc = false;
}
One possibility is to add this to the Validate event:
event.value = util.printf("%,106d", event.value) ;
Note: I have not tested it, but it should work. You might have a look at the util.printf() method description in the Acrobat JavaScript documentation.
Do you actually need the field entry to be a numerical value (i.e., do you need to use the value for mathematical purposes)? If not, you can just set the field to be a text entry, which will allow a user to enter numbers as text in the field, and will thus allow the entry of leading zeros. To do this, go to the following menu: Edit>Edit Text & Images>Forms>Edit>[then click on the field you want to edit]>Edit Fields>Show Field Properties>Format>[then set "Select Field Properties" to "None"]. This change will allow a user to enter any characters in the field, so it's not going to be an option for any form owner/creator who needs to limit the field entry parameters/rules for users. But it works as a quick one-off solution to force leading zeros.
Related
I am trying to do a validation on a textbox value with jquery to make sure textbox accepts only alpha numeric values. I am also trying to allow spaces between words. I am not trying to allow spaces to left and right of the sentence in textbox. how can I allow spaces in middle of words in the textbox?
My trails fiddle
$('#dsTest').keyup(function() {
if (this.value.match(/[^a-zA-Z0-9]/g)) {
this.value = this.value.replace(/[^a-zA-Z0-9]/g, '');
}
});
You're not going to be able to do it gracefully using only keyup, because while still in the process of typing the sentence, the space you just typed (intending it to be in the middle) is at the end.
Instead, I would do something like this:
$('#dsTest').keyup(function() {
if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
}
});
$('#dsTest').focusout(function() {
this.value = this.value.trim();
});
Allow spaces to be typed while typing is in progress, and strip the leading and trailing spaces with String.trim() at some reasonable later point. In my example, I use .focusout(), but you could also just trim when consuming the value.
This is an example of a broader category of validation problems in which testing WHILE input is being entered prevents the user from entering a value that would have been legal once they were done - because entering the value one character at a time requires the value to temporarily have an invalid state. There are two main ways of handling that problem:
Don't test for validation until the user has finished inputting the value
Flag invalid values rather than altering them
You can also combine the two - for instance, highlighting the field while the user is typing to show that the current value is invalid, and then also fixing the value to make it valid if they leave the field while the invalid value is still present.
In pure regex this should work /^[a-zA-Z0-9]+[a-zA-Z0-9\s]*[a-zA-Z0-9]+$/g. Note that this would requires at least 2 characters in the value. If you want to also allow it to be blank then you could do /^([a-zA-Z0-9]+[a-zA-Z0-9\s]*[a-zA-Z0-9]+|)$/.
With all that said, it is prob better usability-wise to just trim the value, as mentioned by other answers, since that does not stop the user from moving forward if they accidentally add a leading or trailing space.
I have a couple of forms with input fields of type number don't seem to have any validation applied by Safari (on Mac OS X Mavericks). That is, if one types letters and hits submits, in every browser a little message appears that you need to put numbers in the field - except Safari. Oh, and Firefox too on this platform.
So, I decided I needed to add validation to my JavaScript code that is handling these form values. I put in a regular expression to reject anything that is not a single-digit number:
if ( !/^([0-9])$/.test(value) ) {
alert ("Please enter a number");
return;
}
In Firefox, this behaves as I hoped. If you enter any letters, or anything other than a single digit, the alert is displayed.
On Safari, if you enter two or more digits, the alert is displayed too, but if you enter non-digit characters, it is not. It acts as if it happily accepts the input, but if I then further add an alert box in an else block below the above to show me what value is, that doesn't get displayed either. There are no errors in the JavaScript console. It's like it just does nothing.
I've stared at the regex a bunch, though it's about as simple as it can be. I'm pretty flummoxed. Any ideas, anyone?
I don't really like regexes. They are always so error prone and you sometimes don't consider all possible matches. Why don't you use something like this:
if (parseInt("15", 10) !== NaN)
alert("is numeric");
else
alert("please enter a number");
Your regex tests true for a string that is a single digit and false (and enters your negated if condition) otherwise. Must it be only one digit or one or more digits?
Add a + to specify one or more numeric values.
/^([0-9]+)$/
Also do you need or are you using the grouping (parenthesis around the digits)?
/^[0-9]+$/
fiddle
Misc ways to validate integer ..
// Integers including 0: following values will return true (0, 1, 2 .., 65, etc.)
/^(?:0|[1-9]\d*)$/.test(value);
// Integers, zero not allowed
/^[1-9]\d*$/.test(value);
// Test whether the value is an integer or not (+ sign try to cast the value into a number)
if(Number.isInteger(+value)){/* ... */}
User friendly solution ..
Few months ago, I had to format a user input that had to be either an integer or a float number ; Formatting process is triggered on keypress.
This is what I achieved Fiddle here
Allowed formats
.80
0.80
15.80
1500
Hope it will help ..
I need to mask a textbox control to allow string value in a pair of 3 character using java script.
Eg:- I have a Text box control zip code in which when a user gives input then it should automatically pair them in a pair of 3 character (ABC#123), and total length of input is 6 Max.
Is it possible then how??
var text="ABC123";
//you can add any validation if you require like to check the length of string
//if(text.length<=6)
var output=text.substring(0,3)+" "+text.substring(3);
check this
I've an HTML field in a form and, using JS and Regex, I must restrict the characters the user can insert in the field. The user of the form can only insert the following characters ( ) * + ^ / X x, and numbers and spaces (when he digits or pastes a different character nothing is written). Is this possible? I need the Regex, I eventually know how to do with JS.
Try this regex
[^\(\)\*+\^/Xx0-9 ]
Here we are trying to find a pattern which contains none of the allowed characters. If you find it this means that the character entered was wrong.
There is no right or wrong answer to this.
Usually it is less expensive to check using the bias of the logic for the particular condition.
If it sounds better saying whats allowed, then use the allowed (positive) character class.
Allowed class: [()*+^/Xx0-9 ], Checks: <space> (-+ /-9 X \^ x
Not allowed class: [^()*+^/Xx0-9 ], Checks: \0-\37 !-' , \- . :-W Y-\] _-w y-\377
Statistically, if %99 of the data enterred were valid, the 'Allowed' class would do less work in that
not every character or range has to be checked.
Where the 'Not-Allowed' class will have to check every range.
In this particular case, the negative class has many more ranges and characters to check, its borderline
more efficient if most of the data were invalid,
With regard to:
[^\(\)\*+\^/Xx0-9 ]
None of those characters need to be escaped inside a set (but it is okay to do so).
If you want to restrict as the user types (this uses jquery methods):
$('#in1').keyup(function (evt) {
var content = $('#in1').val();
$('#in1').val(content.replace(/[^()*+^/Xx0-9]/g, ""));
return true;
});
Where 'in1' is the id of the input.
I am having two text fields in my form. The data to be entered in the field are Name and City respectively. I want to check that the user has not entered any special symbols like !,#,#........ i.e, the only thing user should enter must be belonging to a-z,A-Z, though the user can enter Underscore(_), but no numbers, no special symbols.
I want to check this using JavaScript, how can this be achieved.
Thanks in advance.
A classic problem that's usually solved with the help of regular expressions.
var myString = "London";
if (myString.match(/^[a-zA-Z_]+$/)) {
// Success
}
If you want to allow spaces, like for New York, change the pattern to /^[a-zA-Z_\s]+$/.