validating dxtextbox using regex - javascript

I am trying to validate a text box where user will only be allowed to enter alphabets,number,and few special characters like !, #, #, $, %, &, * currently I tried only [a-zA-Z0-9] which accepts only alphabets And number,can someone please suggest me a regex which will help me to get the same.

/^[a-zA-Z0-9!##$%&*]+$/g should work for you. This will validate if the the complete string is composed of one or more characters found within the square brackets.
To familiarize yourself with some regular expressions syntax and meaning, I would suggest using RegExr. This provides an online validator, explains each element of the expression and has helpful tips/examples in the side bar to help with expression building.

/[a-zA-Z0-9!##$%]$/g.test('aaaaa!jbhjj###')

Related

Email validation doesn't work

I have this simple regular expression for Emails.
/^[a-z]+([\.-_]?[a-z0-9]+)*#([a-z]{3,})+(\.[a-z]{2,3})+$/i;
But when I use this example: first#last#example.com it's still works, And Also when I remove # character from expression :
`/^[a-z]+([\.-_]?[a-z0-9]+)*([a-z]{3,})+(\.[a-z]{2,3})+$/i
it gives the same result.
This expression allows an infinite number of at signs (i.e. #) between at least 2 characters in the email !!
Where is the problem with this expression?
Your pattern is rather restrictive, you might think of other options of validating an email address, like type="email" if it is an input field validation.
As to why the regex matches # even if you take it out, or matches a string with two # symbols, that is cased by [.-_] that matches a lot of chars as the hyphen creates a range that includes #. You need to use [._-] instead.
You may "fix" the regex as
/^[a-z]+([._-]?[a-z0-9]+)*[a-z]{3,}(\.[a-z]{2,3})+$/i
However, this regex is not good to use in real life scenarios.
You want something like that?
/^[a-z\.\-_]+#([a-z]{3,})+(\.[a-z]{2,3})+$/
Probably with sign \.-_ you wanted to have either ".", or "-" or "_" to be used inside the regex, but you forgot to escape "minus".
Or you can use your own but with escape:
^[a-z]+([\.\-_]?[a-z0-9]+)*#([a-z]{3,})+(\.[a-z]{2,3})+$
PS: Remember that a real valid email address could be completely different and has a huge regex, and moreover, each web server defines what is allowed and what is not in email.

Regex password to allow special characters javascript

I am using the following regex (got it from here) to verify passwords:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,}$/
I'm not too familiar with regexpressions, but how would I allow this to allow for special characters like !##$%^&*?
Thanks!
Simply add the other characters you want to the part inside the brackets.
The resulting regex is:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z!##$%^&*?]{6,}$/
Regex101 Tested
I think that this regular expression /^[\w!##\$%\^&\*\?]{8,}$/ will match what you need if the all the special characters you want to include are the ones you mentioned and you want a password of at least 8 characters.

Find e-mails on string

I asked this question a few days ago:
Unable to get a specific value on a JSON object
The first answer was just perfect. Now I need to modify that regular expression in order to find e-mails. The problem is I don't know anything about regular expressions and I've been looking around but don't seem to be able to pull it off.
This is the code:
var m=null
, result=JSON.stringify(response)
, re=/"message":"([^"]+)"/g
, messages=[];
while( m=re.exec(result) ) {
messages.push(m[1]);
Everything is explained on my other question, but basically what this code does is get message":"THIS TEXT"
Now I want to find out whether that text contains an e-mail or not.
I've been checking out many javascript regexp examples and find it rather confusing so if you could give me a little explanation (or something to read) about why it's done the way it's done I'd really appreciate it.
Regexp you are looking for is long and ugly. The email address definition in RFC standard is too sophisticated and permissive. See "Valid email addresses" section on wikipedia. But, you can check whether string looks like email with this simple regexp:
/^.+#.+\..+$/
The explanation of how this works can be found at Regexper
There are some very, very long regexes that you can use to validate emails based on RFC standards. Here is a regex that verifies regexes based on RFC882, which is anything from short and easy-to-understand.
If you wanted to validate anything that looked like an email, you could use this:
^.+#.+\..+$
But, this regex will also allow spaces, and multiple # symbols. So, you could use this:
^[^#]+#[^#]+\.[^#]+$
But this will allow special characters in the name and TLD, so, here's a short regex that will match almost all English emails (as well as those that aren't english):
^([a-zA-Z0-9\-_\~\!\$\&\'\(\)\*\+\,;\:\=]+)\#(.+)\.([a-zA-Z]{2,36})$
This regex will match the characters a-z, A-Z, 0-9, -, _, ~, !, $, &, ', (, ), *, +, ,, ;, :, and = one or more times before the # symbol, will match any characters after the # symbol (almost all characters are now allowed with the new international domains), and allows a-z or A-Z 2 or more times as the TLD.
There are some international domains that do not have english characters in them, so it may be best to replace [a-zA-Z]{2,36} with .{2,36}, if you're expecting and international audience.
Here's a live preview of Regex #1 on regex101.com.
Here's a live preview of Regex #2 on regex101.com.
Here's a live preview of Regex #3 on regex101.com.

regular expressions and unicode

I'm trying to validate this regular expression with JavaScript. What I need it to do is check if at least 2 words are entered. By words I mean strings with any characters except some specific. Everything is good until I end a string with a special unicode character, such as "ā". Then the expression fails to validate. Currently the expression looks like this -
/^([^<>\\\/\?&{};#{}\+\*()"=%#,:0-9]{1,}\w){2,}$/i
Any ideas on how to validate unicode expressions in this case?
On most programming lang, you would try this Unicode Regex:
/^([^<>\\\/\?&{};#{}\+\*()"=%#,:0-9\s]{1,}\p{L}){2,}$/ims
Or like this
/^([^\P]{1,}\p{L}){2,}$/ims
However, JS doesnt support Unicode that easily :(
See here for example: Javascript + Unicode regexes

javascript email regular expression

Can someone explain this regular expression to validate email.
var emailExp = /^[\w\-\.\+]+\#[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
I need to know what does this independent elements do
"/^" and "\" and "\.\-" and "$" //Please explain individually
Thanks in advance
Quick explanation
/
JavaScript regular expressions start with a / and end with another one. Everything in-between is a regular expression. After the second / there may be switches like g (global) and/or i (ignore case) ie. var rx = /.+/gi;)
^
Start of a text line (so nothing can be prepended before the email address). This also comes in handy in multi-line texts.
\
Used to escape special characters. A dot/full-stop . is a special character and represents any single character but when presented as \. it means a dot/full-stop itself. Characters that need to escaped are usually used in regular expression syntax. (braces, curly braces, square brackets etc.) You'll know when you learn the syntax.
\.\-
Two escaped characters. Dot/full-stop and a minus/hyphen. So it means .-
$
End of line.
Learn regular expressions
They are one of the imperative things every developer should understand to some extent. At least some basic knowledge is mandatory.
Some resources
General regular expression syntax resource
http://www.regular-expressions.info/
JavaScript related regular expressions
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
/
The start of the expression
^
The start of the string (since it appears at the start of the expression)
\
Nothing outside the context of the character that follows it
\.\-
A full stop. A hyphen.
$
The end of the string
The other posters have done an excellent job at explaining this regex, but if your goal is to actually do e-mail validation in JavaScript, please check out this StackOverflow thread.

Categories