Regex modification needed to enforce atleast one special symbol character - javascript

I'm using the following regex for a password field :
/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!##$%^&*()_+])[A-Za-z\d][A-Za-z\d!##$%^&*()_+]{7,63}$/
I'd like to enhance it to force atleast one number , one alphabet and one special symbol.
I'm using the following JavaScript code to validate the same :
function validatePassword()
{
var password = document.getElementById('password').value;
var userID = document.getElementById('user_ID').value;
var regexPattern = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!##$%^&*()_+])[A-Za-z\d][A-Za-z\d!##$%^&*()_+]{7,63}$/ ;
if(regexPattern.test(password))
{
if(userID === password )
{
$('#status').text( 'User id is same as password . Please choose a more secure password');
return false;
}
else if(password === reverse(userID))
{
$('#status').text( 'Password is reverse of user id . Please choose a more secure password');
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
How do I do it ?
Thanks.

Instead of using regex you can check like this also.
Just replace
if(regexPattern.test(password))
with
if(regexPattern.test(password) && /[a-zA-Z]/.test(password) && /[0-9]/.test(password) && /[\!\#\#\$\%\^\&\*\(\)\_\+]/.test(password) )
Check string if it is valid password, contain alphabet, number and special character ( if you want more add in range ).

Related

Why this regular expression return false?

i have poor eng, Sorry for that.
i'll do my best for my situation.
i've tried to make SignUpForm using regular expression
The issue is that when i handle if statement using the regular expression
result is true at first, but after that, become false. i guess
below is my code(javascript)
$(document).ready(function () {
var idCheck = /^[a-z]+[a-z0-9]{5,19}$/g; // more than 6 words
var pwCheck = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/; // more than 8 words including at least one number
var emCheck = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/; // valid email check
var signupConfirm = $('#signupConfirm'),
id = $('#id'),
pw = $('#pw'),
repw = $('#repw'),
email =$('#email');
signupConfirm.click(function () {
if(id.val() === '' || pw.val() === '' || email.val() === ''){
$('#signupForm').html('Fill the all blanks');
return false;
} else {
if (idCheck.test(id.val()) !== true) {
$('#signupForm').html('ID has to be more than 6 words');
id.focus();
return false;
} else if (pwCheck.test(pw.val()) !== true) {
$('#signupForm').html('The passwords has to be more than 8 words including at least one number');
pw.focus();
return false;
} else if (repw !== pw) {
$('#signupForm').html('The passwords are not the same.');
pw.empty();
repw.empty();
pw.focus();
return false;
}
if (emCheck.test(email.val()) !== true) {
$('#signupForm').html('Fill a valid email');
email.focus();
return false;
}
}
})
});
after id fill with 6 words in id input, focus has been moved to the password input because the condition is met.
but after i click register button again, focus move back ID input even though ID input fill with 6 words
i've already change regular expression several times. but still like this.
are there Any tips i can solve this issue?
I hope someone could help me.
Thank you. Have a great day
Do not use the global flag on your regexes. Your code should be:
var idCheck = /^[a-z]+[a-z0-9]{5,19}$/;
When you match with the /g flag, your regex will save the state between calls, hence all subsequent matches will also include the previous inputs.
use
var idCheck = /^[a-z]+[a-z0-9]{5,19}$/
removing the g flag
and modify the line
else if (repw.val() !== pw.val()) {

entering regular expressions in javascript

Im trying to add this regular expression ^[a-zA-Z0-9,.&#-]{1-45}#[a-zA-Z]{1-45}.[a-z]{3}$ to validate email addresses to this javascript code.
if(email=="" || email==null)
{
document.getElementById("em_error").innerHTML="*You must enter your Email Address";
error=true;
return false;
}
else
document.getElementById("em_error").innerHTML="";
You can use the match function.
if(email=="" || email==null || !email.match(/^[a-zA-Z0-9,.&#-]{1-45}#[a-zA-Z]{1-45}.[a-z]{3}$/,i))
NOTE
Please review your pattern, because there should be longer and shorter TLD then 3 characters, like .museum, .eu, .nowanytldcantakenformoney
Change your code as shown below (using RegExp.test function):
...
var re = /^[a-zA-Z0-9,.&#-]{1-45}#[a-zA-Z]{1-45}.[a-z]{3}$/;
if (!email || !re.test(email)) { // if the input value is empty or doesn't match the needed pattern
document.getElementById("em_error").innerHTML="*You must enter your Email Address";
error = true;
return false;
} else {
document.getElementById("em_error").innerHTML="";
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Javascript function not working after switching to whirlpool

I've been following a guide to make a secure login page ( given I'm a newbie to PHP, and I'm trying to learn it ); they use javascript as a processor after you've clicked "submit" on the register page, and the login page. However, the function seems to be broken. I've edited it, so that the salt is converted to whirlpool, instead of sha512.
Here's the function ( also, the link to the guide: http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL )
Function:
function formhash(form, password) {
// Create a new element input, this will be our hashed password field.
var p = document.createElement("input");
// Add the new element to our form.
form.appendChild(p);
p.name = "p";
p.type = "hidden";
p.value = whirlpool(password.value);
// Make sure the plaintext password doesn't get sent.
password.value = "";
// Finally submit the form.
form.submit();
}
function regformhash(form, uid, email, password, conf) {
// Check each field has a value
if (uid.value == '' ||
email.value == '' ||
password.value == '' ||
conf.value == '') {
alert('You must provide all the requested details. Please try again');
return false;
}
// Check the username
re = /^\w+$/;
if(!re.test(form.username.value)) {
alert("Username must contain only letters, numbers and underscores. Please try again");
form.username.focus();
return false;
}
// Check that the password is sufficiently long (min 6 chars)
// The check is duplicated below, but this is included to give more
// specific guidance to the user
if (password.value.length < 6) {
alert('Passwords must be at least 6 characters long. Please try again');
form.password.focus();
return false;
}
// At least one number, one lowercase and one uppercase letter
// At least six characters
var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/;
if (!re.test(password.value)) {
alert('Passwords must contain at least one number, one lowercase and one uppercase letter. Please try again');
return false;
}
// Check password and confirmation are the same
if (password.value != conf.value) {
alert('Your password and confirmation do not match. Please try again');
form.password.focus();
return false;
}
// Create a new element input, this will be our hashed password field.
var p = document.createElement("input");
// Add the new element to our form.
form.appendChild(p);
p.name = "p";
p.type = "hidden";
p.value = whirlpool(password.value);
// Make sure the plaintext password doesn't get sent.
password.value = "";
conf.value = "";
// Finally submit the form.
form.submit();
return true;
}

Javascript Email Validation Specific Domain

I'm trying to get someone to use a specific email domain of #mail.fhsu.edu. Here is my Validation code.
function validateFHSUEmail(inputField, helpText) {
if (inputField.value.length == 0) {
if (helpText != null) {
helpText.innerHTML = "Please Enter a Value";
}
return false;
} else {
var reg = /^[a-z.]+#mail.fhsu.edu$/;
if (!reg.test(inputField)) {
if (helpText != null) {
helpText.innerHTML = "Please Enter FHSU Email";
}
Am I calling it wrong or what because no matter what it returns false.
You're testing the variable "inputField", which apparently is a reference to a DOM element. You want inputField.value in the test.
edit Note the comment wherein it's pointed out that your regex should use \. for the periods in the domain name.
If you want valid email and specific domain try this regex:
/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#domain.com$/

jQuery Tools Validator - regex test - exclude just letters

i'm trying to validate a field to include everything except letters but the following only works on the first character i enter. So if i enter '123a' the test method returns true.
$.tools.validator.fn("input#Phone", "Please enter a valid phone number.", function(input, value) {
var pass;
var rgx = /[^a-z]/gi;
if ( rgx.test(value)
|| (value == "")
|| (value == $(input).attr("placeholder"))) {
$(input).removeClass("invalid");
pass = true;
} else {
$(input).addClass("invalid");
pass = false;
}
return pass;
}
You're only matching against a single character.
/^[^a-z]$/i
This ensures that the entire string is non-letters.
for only numeric:
RegExp(/^[^a-zA-Z]$/i)
for phone number you can use
RegExp(/^[0-9 -()+]{6,20}$/i)

Categories

Resources