Java Script - Regex - Password validation with Hebrew characters - javascript

I've got an issue with the password validation.
That's my code:
function validatePassword(){
var password = document.getElementById("password").value;
var re = /^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,16}$/;
if(!password.match(re)){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");
return true;
}
It says that its only invalid, So I thought that its because of the regex.
I asked you if you can help with everything here.
Thanks a lot for helpers!

Try this
// At least eight numbers or/and letters of English or Hebrew language
^[a-zA-Z0-9\u0590-\u05FF]{8,}$
Or
// At least eight characters: one number, one uppercase, one lowercase English letter and one Hebrew letter
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\u0590-\u05FF]).{8,}$
Or
// At least eight characters: one number and one uppercase of lowercase English or Hebrew letter
^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$
Usage:
var p = /^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$/g;
var s = "שלוםWorld2";
if(!p.test(s)){
console.log("Invalid password!");
}
Regex Demo | jsBIn Demo

Maybe you could check it twice to make it easy.
For example:
var password = document.getElementById("password").value;
var re1 = /^a-zA-Z0-9!##$%^&*]{6,16}$/;
var re2 = /[0-9]/
var re3 = /[!##$%^&*]/
if(password.match(re1) && password.search(re2) >=0 && password.search(re3) >=0){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");

Related

passsword validation and alert message in Installshield javascript

Can anyone tell me how to print a message in installshield supported javascript instead of alert?
I tried with the below code but alert is'nt working.
And another problem is that I am unable to validate password by using RegExp.
function Passwordvalidation()
{
var password= Session.Property("PASSWORD");
var patt = new RegExp(":\\[A-Za-z0-9]{6,20}$\\","ig");
var validpassword = patt.test(password);
if(validpassword)
{
GetMD5(); //calls another function
return true;
}
alert("password should have 6 to 20 characters which contains alphabets and digits between 0 to 9");
return false;
}
As #some suggested, you should first only check if the regex works.
But reading the error message "password should have 6 to 20 characters which contains alphabets and digits between 0 to 9" , I think that this part of the regex [A-Za-z0-9]{6,20}$ does not do what you think it does.
This matches any uppercase or lowercase character or digit repeated between 6 and 20 times at the end of the string.
This would also match $$$$$444444 or aaaaaa
For example:
var patt = new RegExp('[a-z0-9]{6,20}$', "i");
var passwords = [
"aaaaaa",
"333333",
"a12b3c",
"AAAAAA",
"$$$$$444444"
];
for (var i = 0; i < passwords.length; i++) {
console.log(passwords[i] + " : " + patt.test(passwords[i]));
}
With ^:
var patt = new RegExp('^[a-z0-9]{6,20}$', "i");
var passwords = [
"aaaaaa",
"333333",
"a12b3c",
"AAAAAA",
"$$$$$444444"
];
for (var i = 0; i < passwords.length; i++) {
console.log(passwords[i] + " : " + patt.test(passwords[i]));
}
The modifier i makes it case insensitive, so you could update your regex to [a-z0-9]{6,20}$ or [A-Z0-9]{6,20}$
You can also omit the g modifier to prevent wrong results.
It is probably because var patt = new RegExp(":\\[A-Za-z0-9]{6,20}$\\","ig"); throws an exception since that is an illegal regexp.
You don't need to use new RegExp but can define it as a literal regexp:
var patt = /^[A-Za-z0-9]{6,20}$/;
Since you defined ranges A-Z and a-z you don't need the i flag, and you don't need the g flag either.
This answer explains password restriction validation with regex really nicely:
Let's say that we want our password to:
Contain between 8 and 15 characters
Must contain an uppercase letter
Must contain a lowercase letter
Must contain a digit
Must contain one of special symbols
Then we can write a regex like this:
^(?=.{8,15}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!##$%^&*]).*$
\__________/\_________/\_________/\_________/\______________/
length upper lower digit symbol

Regular Expression: password must contain at least 1 special character

I am trying to verify that the password inserted by the user has atleast 1 special character in it. Can anyone help me with the regExp?
var password = document.getElementById(.....).value;
var special = new RegExp("?????");
if(special.test(password)){
......
}
I consider all the non-word characters as special characters.
var special = new RegExp("^.*?\\W");
OR
var special = /^.*?\W/;
use this way.
var passwd = document.getElementById('password').value;
var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/);
if (pattern.test(passwd)) {
// code goes here
}

regular expression of number and letters mix

My account must have number and letters.And its length range 4 and 8.
It's not only number and it's not only letters.
Right ex: a111 ,1a1bb, aa111a, 111aaa. Error ex: abcdef, 12345, a123!.
How can I write the Regular Expression.
I tried write that:([a-z]+[0-9]+[a-z]*){4,10}|([0-9]+[a-z]+[0-9]*){4,10}.
But it's not match. Where is my error?
Try this: update ^((?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{4,20})$
Here
^(
(?=.*\d) // must contains one digit from 0-9
(?=.*[a-zA-Z]) //must contains one lowercase Uppercase characters
[a-zA-Z0-9] //match any letter or number previous condition checking
{4,20} //length at least 4 characters and maximum of 20
)$
Live demo
Try this:
/^(?=.[a-z]+[0-9]|[0-9]+[a-z].)[a-zA-Z\d*]{4,8}$/
var filter = /^(?=.*[a-z]+[0-9]|[0-9]+[a-z].*)[a-zA-Z\d*]{4,8}$/;
var vals = "11aa11";
if (filter.test(vals)){
alert('working');
}else {
alert('not working');
}
Try this
var patt = new RegExp("(([A-Za-z][0-9]) |([0-9][A-Za-z])) ([A-Za-z]*[0-9]*){2,6}");
var res = patt.exec(test_str);

validate numeric with 1 special character

i want to validate numeric and allows the + (plus sign), but its not working
what i want
+63443 -> OK
8452 -> OK
s55sd -> Not OK
here's my code
var Nom = $("#addKonId1").val().split(" ").join("").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var intRegex = /^\d+$/;
if (!intRegex.test(Nom)) {
alert("wrong Number");
} else {
alert(Nom);
}
Try This
var Nom = $("#addKonId1").val().trim(" ");
var intRegex = /^\+?\d+$/;
if(!intRegex.test(Nom)) {
alert("wrong Number");
}
else{
alert(Nom);
}
DEMO HERE
The regular expression for what you're looking for is:
^\+?\d+$
Which means "a string beginning with optionally one plus sign followed by one or more digits".
Your regex right now only tests for a string beginning with one or more digit characters. Alter intRegex like so:
var intRegex = /^\+?\d+$/;
On a side note, what you're doing in your first line with the replacing can simply be done with trim():
var Nom = $("#addKonId1").val().split(" ").join("").trim();

javascript for checking alphabets from a string

I have a string which is of format 245545g65.
var value = "245545g65"
var last3Letters = value.substring(7,9); // abc
Now I want to validate whether the last three letters contains only alphabets, if it is alphabet , i want to alert it.how to alert g?
how do i do this?
assuming that "contains only alphabets" means the last three characters are a combination of the letters a-z:
var str = '245545g65';
if (/[a-z]{3}$/.test(str)){
// last three characters are any combinations of the letters a-z
alert('Only letters at the end!');
}
you can use RegEx and compare length
var re = new RegExp("[^0-9]*", "g");
var newlast3Letters =last3Letters.replace(re,"");
if(newlast3Letters.length!=last3Letters.length)
{
alert("not all alphabets");
}
else
{
alert("all alphabets");
}
you can use isNaN to check weather s string is number
if (!isNan(last3Letters))
alert(last3Letters + ' is number.')
else
alert(last3Letters + ' is not number.')
You can also do this:
var value = "245545g65"
if(value.slice(value.length-3).search(/[^a-z]/) < 0) {
alert("Just alphabets");
} else {
alert("Not just alphabets");
}
Easy:
var alpha = /^[A-z]+$/;
alpha.test(last3Letters);
This will return a boolean (true/false). Stolen from here.

Categories