I use following code to check if a user input is lowercase or not. I will allow characters from a to z. no other characters allowed.
JavaScript file:
var pat = /[a-z]/;
function checkname()
{
var t = $("input[name='user_name']").val();
if(pat.test(t) == false)
{
alert('Only lowercase characters allowed');
}
}
//... other functions
But this donot work all the time. If I enter industrialS, it will not find that capital 'S'.
I also tried: /^[a-z]$/ and /[a-z]+/. But not working.
PLease help me.
Your regular expression just checks to see if the string has any lower-case characters. Try this:
var pat = /^[a-z]+$/;
That pattern will only match strings that have one or more lower-case alphabetic characters, and no other characters. The "^" at the beginning and the "$" at the end are anchors that match the beginning and end of the tested string.
if((/[a-z]/.test(email))==true){//allow the small characters}
Your regexp should be:
/^[a-z]+$/
Since all you want is lower case letters, instead of just telling the user s/he's done something wrong, I would fix it:
function checkname() {
var disallowed = /[^a-z]/gi; // g=global , i=case-insensitive
if (this.value == disallowed) {
//delete disallowed characters
this.value = this.value.replace(disallowed,'');
alert('Only lowercase letters allowed');
//instead of an alert, i would use a less intrusive fadeIn() message
}
this.value = this.value.toLowerCase();
}
Related
I'm trying to build a regex which allows the following characters:
A-Z
a-z
1234567890
!##$%&*()_-+={[}]|\:;"'<,>.?/~`
All other characters are invalid. This is the regex I built, but it is not working as I expect it to. I expect the .test() to return false when an invalid character is present:
var string = 'abcd^wyd';
function isValidPassword () {
var regex = /[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*/g
return regex.test(string);
}
In this case, the test is always returning "true", even when "^" is present in the string.
Your regex only checks that at least one of the allowed characters is present. Add start and end anchors to your regex - /^...$/
var string = 'abcd^wyd';
function isValidPassword () {
var regex = /^[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*$/g
return regex.test(string);
}
... another approach, is instead of checking all characters are good, to look for a bad character, which is more efficient as you can stop looking as soon as you find one...
// return true if string does not (`!`) match a character that is not (`^`) in the set...
return !/[^0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]/.test()
Instead of searching allowed characters search forbidden ones.
var string = 'abcd^wyd';
function regTest (string) {//[^ == not
var regex = /[^0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]/g
return !regex.test(string);//false if found
}
console.log(regTest(string));
The regex, as you've written is checking for the existence of the characters in the input string, regardless of where it appears.
Instead you need to anchor your regex so that it checks the entire string.
By adding ^ and $, you are instructing your regex to match only the allowed characters for the entire string, rather than any subsection.
function isValidPassword (pwd) {
var regex = /^[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*$/g\;
return regex.test(pwd);
}
alert(isValidPassword('abcd^wyd'));
Your regexp is matching the first part of o=your string i.e. "abcd" so it is true . You need to anchor it to the start (using ^ at the beginning) and the end of the string (using $ at the end) so your regexp should look like:
^[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!##$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]$
That way it will need to match the entire string.
You can visualize it in the following link:
regexper_diagram
This regex will work.
var str = 'eefdooasdc23432423!##$%&*()_-+={[}]|:;"\'<,>.?/~\`';
var reg = /.|\d|!|#|#|\$|%|&|\*|\(|\)|_|-|\+|=|{|\[|}|]|\||:|;|"|'|<|,|>|\.|\?|\/|~|`/gi;
// test it.
reg.test(str); //true
I use this site to test my regex.
Regex 101
I need a regular expression to validate a password containing at least 8 characters, must include at least one uppercase letter and a lowercase letter. And must specifically include one of the following symbols #,#,%,^,&,*,)
i havent been able to find one that would include only those ascii characters.
thanks in advance for your help!
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*]).{8,}$/
Regular expression to assert a password that must contain atleast one Smallcase ,Capitalcase alphabet and a Special character(!##$%^&*).
Can increase the max length of password from 20 to more.
You can also put all your validation regex's in an array and then use every.
var atLeastLowerCase = /[a-z]/;
var atLeastUpperCase = /[A-Z]/;
var atLeastSpecial = /[\#\#\%\^\&\*\]\)]/;
var password = "somePass#";
var passes = [atLeast8,atLeastLowerCase,atLeastUpperCase,atLeastSpecial].every(function(a){
return a.test(password);
}) && password.length>=8;
if(passes){
//do something
}else{
//do something else
}
Regular expression not able to read complete string, only working correct with single character.
var abc = "ab";
var patter = /^([a-z0-9A-Z])$/;
if (patter.test(abc)) {
console.log('yes');
} else {
console.log('no');
}
You must set a quantifier when you don't want just one character.
Add a * to match zero or more character (or a + if you want to be sure there's at least one character);
var patter = /^[a-z0-9A-Z]*$/;
Note that I removed the parentheses : they're useless with the test method.
I'm using a regex below to validate password to accept alphanumeric characters only. The regex works if I enter 2 characters one alpha and one number but if more than two characters my regex doesn't work. I want if possible the following results as shown in "Expected Behavior". Can anyone help me rewrite my regex?
JavaScript
function checkPasswordComplexity(pwd) {
var regularExpression = /^[a-zA-Z][0-9]$/;
var valid = regularExpression.test(pwd);
return valid;
}
Current Behavior
Password:Valid
a1:true
aa1:false
aa11:false
Expected Behavior
Password:Valid
aa:false (should have at least 1 number)
1111111:false (should have at least 1 letter)
aa1:true
aa11:true
a1a1a1a1111:true
You want to add "one or more", you're currently checking for a letter followed by a number.
Try:
/^[a-zA-Z0-9]+$/
+ means 'one or more'
I also joined the ranges.
Note: I don't understand why you'd want to limit the password to such a small range though, having a wide character range will make your passwords stronger.
Here is a fiddle demonstrating the correct behavior
If you just want to validate that the password has at least one letter and at least one number, you can check like this:
function checkPasswordComplexity(pwd) {
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
var valid = number.test(pwd) && letter.test(pwd); //match a letter _and_ a number
return valid;
}
function checkPasswordComplexity(pwd) {
var regularExpression = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
var valid = regularExpression.test(pwd);
return valid;
}
You can use this:
/^(?=.*\d)(?=.*[a-z])[a-z\d]{2,}$/i
Try doing this:
var regularExpression = /^[a-zA-Z0-9]+$/;
This means "one or more letter or number."
However, some users might also want to enter symbols (like &*#) in their passwords. If you just want to make sure there is at least one letter and number while still allowing symbols, try something like this:
var regularExpression = /^(?=.*[a-zA-Z])(?=.*[0-9]).+$/;
The (?=.*[a-zA-Z]) is a positive lookahead. This means that it makes sure that there is a letter ahead of it, but it doesn't affect the regex.
{
var pwd=document.getElementById('pwd').value;
var reg = /^[a-zA-Z0-9]{8,}$/;
var re=reg.test(pwd);
alert(re);
}
I think lookaround aren't supported by javascript, so you can use:
^([a-zA-Z]+\d+)|(\d+[a-zA-Z]+)
But if they are supported:
/^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]{2,}$/
I am trying to validate a string, that should contain letters numbers and special characters &-._ only. For that I tried with a regular expression.
var pattern = /[a-zA-Z0-9&_\.-]/
var qry = 'abc&*';
if(qry.match(pattern)) {
alert('valid');
}
else{
alert('invalid');
}
While using the above code, the string abc&* is valid. But my requirement is to show this invalid. ie Whenever a character other than a letter, a number or special characters &-._ comes, the string should evaluate as invalid. How can I do that with a regex?
Add them to the allowed characters, but you'll need to escape some of them, such as -]/\
var pattern = /^[a-zA-Z0-9!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/
That way you can remove any individual character you want to disallow.
Also, you want to include the start and end of string placemarkers ^ and $
Update:
As elclanrs understood (and the rest of us didn't, initially), the only special characters needing to be allowed in the pattern are &-._
/^[\w&.\-]+$/
[\w] is the same as [a-zA-Z0-9_]
Though the dash doesn't need escaping when it's at the start or end of the list, I prefer to do it in case other characters are added. Additionally, the + means you need at least one of the listed characters. If zero is ok (ie an empty value), then replace it with a * instead:
/^[\w&.\-]*$/
Well, why not just add them to your existing character class?
var pattern = /[a-zA-Z0-9&._-]/
If you need to check whether a string consists of nothing but those characters you have to anchor the expression as well:
var pattern = /^[a-zA-Z0-9&._-]+$/
The added ^ and $ match the beginning and end of the string respectively.
Testing for letters, numbers or underscore can be done with \w which shortens your expression:
var pattern = /^[\w&.-]+$/
As mentioned in the comment from Nathan, if you're not using the results from .match() (it returns an array with what has been matched), it's better to use RegExp.test() which returns a simple boolean:
if (pattern.test(qry)) {
// qry is non-empty and only contains letters, numbers or special characters.
}
Update 2
In case I have misread the question, the below will check if all three separate conditions are met.
if (/[a-zA-Z]/.test(qry) && /[0-9]/.test(qry) && /[&._-]/.test(qry)) {
// qry contains at least one letter, one number and one special character
}
Try this regex:
/^[\w&.-]+$/
Also you can use test.
if ( pattern.test( qry ) ) {
// valid
}
let pattern = /^(?=.*[0-9])(?=.*[!##$%^&*])(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9!##$%^&*]{6,16}$/;
//following will give you the result as true(if the password contains Capital, small letter, number and special character) or false based on the string format
let reee =pattern .test("helLo123#"); //true as it contains all the above
I tried a bunch of these but none of them worked for all of my tests. So I found this:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$
from this source: https://www.w3resource.com/javascript/form/password-validation.php
Try this RegEx: Matching special charecters which we use in paragraphs and alphabets
Javascript : /^[a-zA-Z]+(([\'\,\.\-_ \/)(:][a-zA-Z_ ])?[a-zA-Z_ .]*)*$/.test(str)
.test(str) returns boolean value if matched true and not matched false
c# : ^[a-zA-Z]+(([\'\,\.\-_ \/)(:][a-zA-Z_ ])?[a-zA-Z_ .]*)*$
Here you can match with special char:
function containsSpecialChars(str) {
const specialChars = /[`!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
return specialChars.test(str);
}
console.log(containsSpecialChars('hello!')); // 👉️ true
console.log(containsSpecialChars('abc')); // 👉️ false
console.log(containsSpecialChars('one two')); // 👉️ false