I would like to check few of my text boxes that must satisfy the following conditions:
Alphabets i meant are from a-z(uppercase and lower case) numbers 0-9 and special characters are ~`!##$%^&*()-_+={}[];:'",.<>/?
It can contain only alphabets
It cannot contain only numbers
It cannot contain only special characters
It cannot contain only numbers and special characters
It can contain alphabets,numbers and special characters
It can contain alphabets and numbers
It can contain alphabets and special charcters
I found a solution but seems not working for me:
/^[a-z0-9/. -!##$%^&*(){}:;"',/?]+$/i
I am checking it as:
var alpha=/^[a-z0-9/. -!##$%^&*(){}:;"',/?]+$/i;
if (!alpha.test(username.value))
{
alert('Invalid username');
document.theForm.username.focus();
return false;
}
The problem can be restated as that of matching a string containing ONLY the characters
A-Za-z0-9~`!##$%^&*()-_+={}[];:'",.<>/?
such that at least one of them is a letter.
Fortunately, you've covered all the printable characters in the range U+0021 to U+007F, so that the desired regex is simply
[!-~]*[A-Za-z][!-~]*
EDIT: On closer reading, I noticed you did not allow the backslash! If you want to allow the backslash, the regex above is okay; if not, you should modify it like so:
[!-\[\]-~]*[A-Za-z][!-\[\]-~]*
It's a bit uglier, because to exclude the backslash we have to say
All characters in the range ! to [ union characters in the range ] to ~, and the explicit mention of [ and ] requires escaping with, you guessed it, the \.
Hopefully you meant to allow the \ so you can use the simpler regex above.
EDIT 2
To make the regex more efficient, you should use a reluctant quantifier (as kcsoft did):
[!-~]*?[A-Za-z][!-~]*
Also for JavaScript, but not for Java if you are using matches, you should anchor the regex to match the whole string, giving this in JavaScript:
/^[!-~]*?[A-Za-z][!-~]*$/
And, as you did in your question, you can shorten it a bit more by using the i modifier:
/^[!-~]*?[A-Z][!-~]*$/i
Can you give some input examples. Can you try this?
/.*?[a-zA-Z]+.*/
Or if you need to specify the list of special characters:
/[list of chars]*?[a-zA-Z]+[list of chars]*/
Related
I am trying to check if a form input is valid based on a regex pattern using javascript.
The form input should look like this: xxxx-xxx-xxxx allowing for both numbers and letters
Right now it only works with digits as I have it setup which is now being changed to allow letters as well. is there a way to change the regex I have to allow numbers and letters and still format as is?
4 letters or numbers A DASH 3 letters or numbers A DASH 4 letters or numbers
validation rule
medNumber: [
{
required: true,
pattern: /^\d{4}?[- ]?\d{3}[- ]?\d{4}$/,
message: "Please enter your #.",
trigger: ["submit", "change", "blur"]
}
],
[A-Za-z0-9] will match only alphanumeric characters.
/^[A-Za-z0-9]{4}?[-]?[A-Za-z0-9]{3}[-]?[A-Za-z0-9]{4}$/
I've also removed the spaces from within your instances of [- ] because that will allow matches such as
xxxx xxx xxxx
In your spec you mention a dash is required, not a space.
The rule \d allows only for digits. So you need to change every occurrence of this to \w, which allows any alphanumeric character (letters and digits). So you'd get the following:
/^\w{4}?[- ]?\w{3}[- ]?\w{4}$/
For future reference, I'd suggest looking at regexone.com. It has helped me a ton with learning all the regex rules. You can also use regex101.com for easy testing of regex patterns.
Edit:
I was probably a bit too quick on this one. As #David mentioned in the comment, \w also includes underscores. If you don't want that, you should look at his answer instead :)
I want to create a regular expression which will:
not contain any space and special characters except "-" and "_"
it should contain at least one alphabet character
The regular expression I created is:
^[^/\s/]+[a-z]{1,}[0-9]*[\-\_]*[^\/][^/\s/]$
It only only matches if my string contains at least 4 characters, including 1 alphabet. I tried it on https://regex101.com/#javascript Can someone help me what I am doing wrong here.
You need to learn about lookaround. One solution to your problem is:
/(?=^[\w-]{4,}$)(.*[a-z].*)/gmi
(?=^[\w-]{4,}$) will assert that you input will contains only chars in the range a-z, digit, _ and , - with a length of at least 4.
(.*[a-z].*) ensure that there will be at least one char in the range a-z.
See Demo
Using fundamental regex primitives, you can use this:
/^[0-9_-]*[a-z]+[0-9a-z_-]*$/i
It works correctly with these sample input strings:
999c123-
a123-88asd
9923--_b
B
99-luftballoons
Z8f
And does not match these strings:
999
-51-
---_-
It's fast and will work in pretty much every regex engine, even non-standard (non-extended) grep.
I have a regular expression in JavaScript to allow numeric and (,.+() -) character in phone field
my regex is [0-9-,.+() ]
It works for numeric as well as above six characters but it also allows characters like % and $ which are not in above list.
Even though you don't have to, I always make it a point to escape metacharacters (easier to read and less pain):
[0-9\-,\.+\(\) ]
But this won't work like you expect it to because it will only match one valid character while allowing other invalid ones in the string. I imagine you want to match the entire string with at least one valid character:
^[0-9\-,\.\+\(\) ]+$
Your original regex is not actually matching %. What it is doing is matching valid characters, but the problem is that it only matches one of them. So if you had the string 435%, it matches the 4, and so the regex reports that it has a match.
If you try to match it against just one invalid character, it won't match. So your original regex doesn't match the string %:
> /[0-9\-,\.\+\(\) ]/.test("%")
false
> /[0-9\-,\.\+\(\) ]/.test("44%5")
true
> "444%6".match(/[0-9\-,\.+\(\) ]/)
["4"] //notice that the 4 was matched.
Going back to the point about escaping, I find that it is easier to escape it rather than worrying about the different rules where specific metacharacters are valid in a character class. For example, - is only valid in the following cases:
When used in an actual character class with proper-order such as [a-z] (but not [z-a])
When used as the first or last character, or by itself, so [-a], [a-], or [-].
When used after a range like [0-9-,] or [a-d-j] (but keep in mind that [9-,] is invalid and [a-d-j] does not match the letters e through f).
For these reasons, I escape metacharacters to make it clear that I want to match the actual character itself and to remove ambiguities.
You just need to anchor your regex:
^[0-9-,.+() ]+$
In character class special char doesn't need to be escaped, except ] and -.
But, these char are not escaped when:
] is alone in the char class []]
- is at the begining [-abc] or at the end [abc-] of the char class or after the last end range [a-c-x]
Escape characters with special meaning in your RegExp. If you're not sure and it isn't an alphabet character, it usually doesn't hurt to escape it, too.
If the whole string must match, include the start ^ and end $ of the string in your RegExp, too.
/^[\d\-,\.\+\(\) ]*$/
I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/
I want to validate any string that contains çÇöÖİşŞüÜğĞ chars and starting at least 5 chars.String to validate can contain spaces.RegEx must validate like "asd Çğ ğT i" for example.
Any reply will helpful.
Thanks.
You can use escape sequences of the form
\uXXXX
where each "X" can be any hex digit. Thus:
\u0020
is the same as a plain space character, and
\u0041
is upper-case "A". Thus you can encode the Unicode values for the characters you're interested in and then include them in a regex character class. To make sure the string is at least five characters long, you can use a quantifier in the regex.
You'll end up with something like:
var regex = /^[A-Za-z\u00nn\u00nn\u00nn]{5,}$/;
where those "00nn" things would be the appropriate values. As to exactly what those values are, you should be able to find them on a reference site like this one or maybe this one. For example I think that "Ö" is \u00D6. (Some of your characters are in the Unicode Latin-1 Supplement, while others are in Latin Extended A.)