Needs to only contain [a-zA-Z0-9.], and followed by an # then the same match afterward, the match before, and after the # shouldn't be any longer than 64 characters long, and at least one length.
^([a-zA-Z0-9\-\.]+){1,64}#([a-zA-Z0-9\-\.]){1,64}$
This seems to work but it sometimes takes forever, why is this?
I'm not sure why you have the + operator in the first part of the regex. I hope this could be useful for you
^([a-zA-Z\d\.]{1,64})#([a-zA-Z\d\.]{1,64})$
For email regex you should use http://emailregex.com/ which provides this regex
/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
also, if you can you should usually just use
<input type="email">
Related
I am a newbie to regex and would like to create a regular expression to check usernames. These are the conditions:
username must have between 4 and 20 characters
username must not contain anything but letters a-z, digits 0-9 and special characters -._
the special characters -._ must not be used successively in order to avoid confusion
the username must not contain whitespaces
Examples
any.user.13 => valid
any..user13 => invalid (two dots successively)
anyuser => valid
any => invalid (too short)
anyuserthathasasupersuperlonglongname => invalid (too many characters)
any username => invalid because of the whitespace
I've tried to create my own regex and only got to the point where I specify the allowed characters:
[a-z0-9.-_]{4,20}
Unfortunately, it still matches a string if there's a whitespace in between and it's possible to have two special chars .-_ successively:
If anybody would be able to provide me with help on this issue, I would be extremely grateful. Please keep in mind that I'm a newbie on regex and still learning it. Therefore, an explanation of your regex would be great.
Thanks in advance :)
Sometimes writing a regular expression can be almost as challenging as finding a user name. But here you were quite close to make it work. I can point out three reasons why your attempt fails.
First of all, we need to match all of the input string, not just a part of it, because we don't want to ignore things like white spaces and other characters that appear in the input. For that, one will typically use the anchors ^ (match start) and $ (match end) respectively.
Another point is that we need to prevent two special characters to appear next to each other. This is best done with a negative lookahead.
Finally, I can see that the tool you are using to test your regex is adding the flags gmi, which is not what we want. Particularly, the i flag says that the regex should be case insensitive, so it should match capital letters like small ones. Remove that flag.
The final regex looks like this:
/^([a-z0-9]|[-._](?![-._])){4,20}$/
There is nothing really cryptic here, except maybe for the group [-._](?![-._]) which means any of -._ not followed by any of -._.
I'm trying to do a search for a character in a string NOT matching the regex :
password.search(/[`!###$%^&*A-Za-z0-9]/i));.
Basically, all characters that aren't this regex isn't allowed and I want to know if the user has input any characters that isn't allowed. For example, '\', or any other characters that I might not think of.
I'm pretty sure there's a question similar to this out somewhere, but despite trying to look for it I surprisingly couldn't find it. If this is a duplicate question please link me.
According to this answer, you could use ?!:
console.log("valid$\\".search(/(?![`!###$%^&*A-Za-z0-9])/i));
console.log("256)128".search(/(?![`!###$%^&*A-Za-z0-9])/i));
f you want to exclude a set of characters (some punctuation characters, for example) you would use the ^ operator at the beginning of a character set, in a regex .
After returning true or false with:
return (/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,8})+$/.test(str));
Where str is testing123#testing123.testing123
it takes about 25 seconds to complete.
In general, shorter strings take less than 1 second.
This is most likely due to back-tracking. I am not very good with Regex, can someone help me reduce the time it takes to process an email. E.g. it must have letter(s) then # then letter(s) then . then letter(s) and must not be too long.
Just use
\S+#\S+
Or even (with anchors)
^\S+#\S+$
and actually send an email to that address rather than using a complicated, likely error-prone expression.
This is the RFC 2822 Standrard for matching emails. It can match 99.9% of emails out today.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
If you want to just catch syntax errors, you can simply use
\S+#\S+
Taken from one of the answers from another question.
Remove ? after each [.-]:
/^\w+(?:[.-]\w+)*#\w+(?:[.-]\w+)*(?:\.\w{2,8})+$/
See the regex demo
In ([.-]?\w+)*, the [.-]? matches 1 or 0 occurrences of . or -, and the whole group pattern gets reduced to (\w+)* after \w+, it causes too many redundant backtracking steps.
Also, it is a good idea to use non-capturing groups if you are only using the grouping construct to quantify a group of subpatterns.
Now, regarding
it must have letter(s) then # then letter(s) then . then letter(s) and must not be too long
I see others suggest ^\S+#\S+\.\S+$ like solutions, which is a good idea, just make sure you understand that \S matches any char other than whitespace (not just letters). Besides, this does not actually provide the final solution since "must not be too long" condition is not met (+ matches from 1 to quite many occurrences, that is why it is described as 1 or more).
I suggest using the pattern inside an HTML5 pattern attribute and restrict the number of chars a user can type with maxlength attribute:
input:valid {
color: black;
}
input:invalid {
color: red;
}
<form name="form1">
<input pattern="\S+#\S+\.\S+" maxlength="256" title="Please enter an email address like name#myhost.com!" placeholder="name#myhost.com"/>
<input type="Submit"/>
</form>
NOTE: the pattern regex is compiled by enclosing the pattern with ^(?: and )$, you do not need to use ^ and $ in the regex here. So, pattern="\S+#\S+\.\S+" is translated into:
^(?: (this is added by HTML5) - start of a string (and a non-capturing group starts)
\S+ - any 1 or more non-whitespace chars
# - a # char
\S+ - any 1 or more non-whitespace chars
\. - a dot
\S+ - any 1 or more non-whitespace chars
)$ (this is added by HTML5) - the non-capturing group ends and the end of a string is matched.
You can use this Regex to check emails :
var emailregex = /^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
replace ()* with ()?
PS: Pretty weird expression to match emails:)
An efficient method of matching emails is:
\S+#\S+\.\S+
It's short, matches almost any email, and won't match:
abc#abc
as some of these other answers might.
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.
I am working on a php+javascript based project and have already made up a mockup page at :
my website
I knew how to use javascript or php to check whether a particular field of form is "empty"or not, that is, whether it contains alphanumerical characters other than whitepsace characters(for instance, space, tab and newline).
However, my normal apporach no longer works since the jquery plugin that I am using now relies on regex to validate the fields.
If you go to the third tab(3. Fill up Shipping Info and Make Payment), you can enter something into the Firstname field and it does the check automatically. Fine. However, if
you just simply put some space characters there and jump to the next field, well, it still feels okay for that, which is not correct since no one's first name is nothing!
The problem? At the back it has a regex like this :
"noSpecialCaracters":{
"regex":"/^[0-9a-zA-Z ]+$/",
"alertText":"* No special caracters allowed"},
This would not filter out empty characters.
I searched online and tried my best to make up another regex to match, I tried
"regex":"/^[^]+$/"
for matching non-empty characters, but that will not do...
Can anyone help me out? Many thanks in advance!
Try this for non-whitespace:
([^\s]*)
Example:
/([^\s])/.test(" A"); //TRUE
/([^\s])/.test(" "); //FALSE
function anyChar(str){
return /\S+/.test(str);
}
will return false if emty data
I'm using
/^\s*\S+.*/
which means
zero or more whitespace characters (\s*), followed by
one or more non-whitespace characters (\S+), followed by
anything at all, whitespace or not (.*).
This allows a single word or multiple words. I'm allowing whitespace at the beginning because I know how easy it is to miss a single space at the beginning and be really confused as to why your input isn't allowed :|
The Mozilla Developer Network has a great JavaScript regex page for reference.
You may want to try wrapping your expression in the following ^\s*(expression)\s*$. Then use the groups to find the "trimmed" matches. This eliminates only trailing or leading whitespace.
You can force the user to enter trimmed text or you can gracefully accept untrimmed input (better) as I find copying and pasting text often leaves some trailing or leading whitespace which the user may be unaware of.
To answer your question, the minimal regex is /\S/ which will match as long as there is at least one non-whitespace character.
However, you probably don't want someone to put in a first name of '12345' or '!!!', so it might be better to use /[a-z]/i as this regex will only match if there is at least one alphabetical character.
/^\s*[0-9a-zA-Z][0-9a-zA-Z ]*$/
that ensures that at least one character is not whitespace and is of one of the allowed characters.
You may also want to consider other characters like hyphen(-) or apostrophe(') that may also appear in names...
/^\s*[0-9a-zA-Z][0-9a-zA-Z '-]*$/