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]+$/.
Related
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.
I have this code:
if(address.length<=0)
{
msg.setAttribute("style", "color:red");
msg.innerHTML='Please enter address';
return false;
}
I would like to change so it checks whether the webform contains BOTH numbers and letters. Can you help me?
Thank you so much,
Jones
p.s.: So I want to make sure they also enter street name AND house number as well (example: 24 Sunshine street would be good, but if they forget house number, they would get the message).
That doesn't look like PHP at all. More like JavaScript...
Here's one way to do it in JavaScript:
var re = /^\d+\s+\D+$/;
if (re.test(address)) {
//We get here if the address is correctly formated
}
else {
//We get here if the string is badly formated
}
The regex works like this:
\d+ matches to one or more numbers
\s+ matches to one or more spaces
\D+ matches to one or more letters
If you want to accept both "24 Sunshine" and "Sunshine 24" you could instead use this:
/^(\d+\s+\D+)|(\D+\s+\d+)$/
And if we want to be extra safe and protect from the case that the user might enter an extra trailing or leading space we could either trail the string or use this ReGex:
/^\s*(\d+\s+\D+)|(\D+\s+\d+)\s*$/
Apart from regular expression which is a very nice and clear solution you can use these php functions:
first the ctype_alnum () in order to check if your string contains letters and digits and then
this on ctype_alpha() in case the above is true to check if user forgot to enter number.
In case you are interested there is also this one ctype_digit() for checking if user missed the address but gave the number.
Or if you want just a regex this it will do the job:
^[a-zA-Z]([a-zA-Z-]+\s)+\d{1,4}
I have a signup form where user can enter, along other information, company's name and company's URL ID. Now, I'd like to suggest "URL ID" to the user so when he types "name", "URL ID" should be based on this input (very similar to Facebook "name" and "username" paradigms). I should also mention that user can manually type in "URL ID" for corrections after it was suggested.
It's important to mention here that based on this question I was able to successfully implement server-side validation for the "name" field using the following Regex expression (client-side validation is not needed):
/^[\p{L}\p{N}]+(?:[- \'\x26][\p{L}\p{N}]+| [\x26] [\p{L}\p{N}]+)*$/iu
There are certain rules that must be applied to URL IDs:
Valid characters are: both lower- and uppercase letters (Latin only), numbers, dashes, underlines and dots
Must begin and end with a letter or number
Must not have more than one special character (dash, underline or dot) in row
Can have multiple special characters in "URL ID"
As an example of what's valid and what's not, here are some examples:
Valid inputs
myusername
my.username12
my.user20_name
8-user-name-my
Invalid inputs
myuser20name.
_myusername10
my..username
myuser-.name
To make the long story short, two things need to be done:
while user is typing in/pasting the "name" field, I need to take input on-the-fly, keep only allowed characters discarding the rest, and filling in the "URL ID" field with the filtered input
while user is typing in/pasting the "URL ID" field, I need input to validates against the rules mentioned above so that for example typing in "my.user-" would be ok (although it would fail the server-side validation due to "-" being the last character), but typing additional "-" would not be allowed
I guess I only need the valid Regex expression, I'm able to code the rest myself. Any help would be appreciated.
I do not know if I understand all the rules, but according to his examples this regexp validates exactly what you need!
^[a-zA-Z0-9]+([-_\.][a-zA-Z0-9]+)*[a-zA-Z0-9]$
http://regexr.com?36f5b
I am try to validate a form to make the user enter their full name like the following..
First, Last
The user must have some string of alphabetic only chars, then a comma, then a space, then a last name, which can again be any string of chars.
This is my current regex..
var alphaExp = /^[a-zA-Z,+ a-zA-Z]+$/;
However, it lets the user submit something as simple as john. I want it to force them to submit something such as john, smith.
What you are doing is creating a character class ([]) that matches any of the following characters: a-zA-Z,+. This allows john to match the whole regex.
This is what you want:
/^[a-zA-Z]+, [a-zA-Z]+$/
However, I would like to advise you that making assumptions about names is a little wrong. What if some guy is named John O'Connor? Or Esteban Pérez? Or just Ng (only a first name)?
"Code that believes someone’s name can only contain certain characters is stupid, offensive, and wrong" - tchrist
Sure, you don't want to let people to enter just gibberish, but leave an option for users to enter something that doesn't necessarily fit your idea of correctness, but is nonetheless correct.
That's not how character sets work:
/^[a-zA-Z]+, [a-zA-Z]+$/
Things to consider:
Any validation you do on the client can be bypassed
Some people may have names with accented letters
Some cultures don't use just two names
^[a-zA-Z]+, [a-zA-Z]+$
Should work, however, do you want to prevent '.'? As in J. Hooker? And more words, like 'Jan van Hoogstrum'? Note also that you are preventing any accented characters being used. An option (although it allows underscores) is to use \w:
^(\w+ )$
Which would give you 'name name name...'. 'First, last' is not a common way to enter your name, so you'll frustrate a lot of users that way.
The correct regexp for allowing only the first letter to be capital would be:
/^[A-Z][a-z]+, [A-Z][a-z]+$/
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.