Javascript check for 1 special character and 2 digits - javascript

How to create a javascript validation for a password field which must contain at least one special character and at least two digits ?

Exact regular expression that perfect match to your query is below, it is tested ...
^(?=.*?[0-9].*?[0-9])(?=.*[!##$%])[0-9a-zA-Z!##$%]{8,}$

function check(str){
var temp = str;
if(/^[a-zA-Z0-9- ]*$/.test(str) == false && temp.replace(/[^0-9]/g,"").length>1) return true;
return false;
}

Related

javascript .match function

I have the following javascript .match script to allow telephone numbers in form submit
var number = jQuery('#phone-number').val();
if ((number.match(/(\d)\1\1\1\1\1/))
|| (number.match(/(\d)(\d)\1\2\1\2\1\2/))
|| (number.match(/123456|234567|345678|456789|567890|987654|876543|765432|654321|543210/))
|| (!number.match(/^(0\d{8,10})?$/))) {
alert("Please supply a valid phone number");
return false;
}
Currently, it doesnt allow a SPACE between numbers.. I'm no good at regex and was wondering if someone could tell me how I allow a SPACE between any number using the script above?
thanks
Craig.
If you want to specify any number of spaces between each character, you can use \s*.
\s stands for whitespace character and * for any number of those
E.g.
\s*(\d)\s*\1\s*\1\s*\1\s*\1\s*\1\s*
const regex = /\s*(\d)\s*\1\s*\1\s*\1\s*\1\s*\1\s*/;
const tel1 = '111111';
const tel2 = ' 1 1 1 1 1 1';
console.log(regex.test(tel1));
console.log(regex.test(tel2));
Ugly, but:
if ((number.match(/(\d)\s*\1\s*\1\s*\1\s*\1\s*\1\s*/))
|| (number.match(/(\d)\s*(\d)\s*\1\s*\2\s*\1\s*\2\s*\1\s*\2\s*/))
|| (number.match(/123456|234567|345678|456789|567890|987654|876543|765432|654321|543210/))
|| (!number.match(/^(0(?:\s*\d\s*){8,10})?$/))) {
alert("Please supply a valid phone number");
return false;
}
For 1 space only replace \s* with \s?
You can remove all spaces from a string with
str = str.replace(/\s/g, '');
Then you can use your existing code.

validation function doesn't work

I have created a java script function, it should validate the input character that should contain 10 characters and can contain alphanumeric characters, but this function does not work, please help me
function ValidateNIC(id)
{
var letters = /^[0-9a-zA-Z ]+$/;
while(id.value.length==10)
if(id.value.match(letters))
{
return true;
}
else
{
alert('NIC must have alphanumeric characters only or should contain 10 charaters');
id.focus();
return false;
}
}
With your code as it stands, if the length is not 10, then nothing else happens. A better approach might be:
if ((id.value.length == 10) && id.value.match(letters)) {
return true;
}
alert("NIC must ...");
id.focus();
return false;
You can put all the conditions for validation in Regex like ^[a-zA-Z0-9]{10}$. Note that additional {10} in the regex pattern string for creating a match only when the length is 10 exactly.
Then you can make use of the Regex Object test method, which test the regex pattern against a string and returns true if the match is successful and false otherwise.
Complete modified snippet below with positive and negative test cases.
function ValidateNIC(id){
var aphaPattern10 = /^[a-zA-Z0-9]{10}$/g;
var result = aphaPattern10.test(id.value);
if(!result){
alert('NIC must have alphanumeric characters only or should contain 10 charaters');
//id.focus();
}
return result;
}
var testObjPass = { value : "012345678a"}
console.log(ValidateNIC(testObjPass));
var testObjFail = { value : "012345678a21312"}
console.log(ValidateNIC(testObjFail));
The following code checks the following
NIC must have alphanumeric characters only or should contain 10 charaters.
So if it is only 10 characters then it will not alert else, it will test the regex. Considering id is an object with key value
function ValidateNIC(id)
{
var letters = /^[0-9a-zA-Z ]+$/;
if(id.value.length!==10){
if(id.value.match(letters))
{
return true;
}
else
{
alert('NIC must have alphanumeric characters only or should contain 10 charaters');
id.focus();
return false;
}
}
}

Match special characters regex in Javascript (Random place)

I'm trying to make a regex to match atleast two special characters,
for a password strength checker. I now have this in Javascript:
if (password.match(/([^A-Za-z0-9]{2,})/)) {
//Add strength
}
But this checks that atleast two special characters that needs to be after each other. How can I make it, so that it will also check if it's not after each other?
Example:
_aaa!* //Match
a!a_a* //Also match
One way to do it:
var password = 'a!a_a*';
var matches = password.match(/([^A-Za-z0-9])/g);
if (matches && matches.length >= 2) {
console.log('Good');
} else {
console.log('Bad');
}
console.log(matches);
You could use replace for this:
var password = 'a!a_a*';
var specialChars = password.replace(/[A-Za-z0-9]/g, '');
console.log(password, specialChars.length > 1 ? 'OK' : 'Too few special chars');
^(.*?[\*\& list all the special characters you want to allow here prefixed by \]){2,}.*$
You can test it here: https://regex101.com/

Regex Expression validation in javascript

Regex to check the first character is in uppercase and allow only alphanumeric,not allow the special charcter.
Thank you Advance
function checkName(val) {
var alpha = document.getElementById(val).value;
var filter = /^[a-zA-Z0-9 ]*$/;
if (!filter.test(alpha)) {
alert("Please Enter Alphanumeric Only");
return false;
}
return true;
}
i Used This its working properly for checking alphanumeric but for
first character uppercase its not working
where can i modify my regex expression or any solution.
try
/^[A-Z][a-zA-Z0-9]+/
For example
/^[A-Z][a-zA-Z0-9]+/.test("Asd");

How do I use a regular expression to match a fixed-length number with a hyphen in the middle?

I am new to regular expressions and wanted to know how to write a regular expression that does the following:
Validates a string like 123-0123456789. Only numeric values and a hyphen should be allowed. Also, verify that there are 3 numeric chars before the hyphen and 10 chars after the hyphen.
The given answers won't work for strings with more digits (like '012-0123456789876'), so you need:
str.match(/^\d{3}-\d{10}$/) != null;
or
/^\d{3}-\d{10}$/.test(str);
Try this:
^\d{3}-\d{10}$
This says:
Accept only 3 digits, then a hyphen, then only 10 digits
Sure, this should work:
var valid = (str.match(/^\d{3}-\d{10}$/) != null);
Example:
> s = "102-1919103933";
"102-1919103933"
> var valid = s.match(/\d{3}-\d{10}/) != null;
> valid
true
> s = "28566945";
"28566945"
> var valid = s.match(/\d{3}-\d{10}/) != null;
> valid
false

Categories

Resources