473,473 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regex for password validation - convert PHP regex to JavaScript regex

14 New Member
can anyone give me a regex to validate the password with following conditions

1. password should have atleast one alphabet and atleast one digit.
2.password should be atleast 6 characters and maximum of 8.
3. also password can allow specialcharacters but it is not mandatory.
without spl.characters also password is valid but it should have atleast one alphabet and minimum 1 digit.
hope i am clear.
i tried with ctype_alnum() function in php but it is accepting if all characters or either alphabet or digit. but i want to enforce atleast one alphabet and one digit should be in password

can any one give me a regex for this pls.....
Dec 1 '08 #1
9 6600
kummu4help
14 New Member
at present i am trying with the following code
function isAlphaNumeric($sInput)
{
if(false===(preg_match("/[a-zA-Z]/", $sInput) > 0))
{
return false;
}
if(false===(preg_match("/[0-9]/", $sInput) > 0))
{
return false;
}
return true;
}
can anyone improve it to suite my requirements which i specified above
Dec 1 '08 #2
kummu4help
14 New Member
now i am trying with the following
Expand|Select|Wrap|Line Numbers
  1.     if(false===(preg_match("/^(?=.*\d)(?=.*[a-z]).{6,8}$/", $sInput) > 0))
  2.     {
  3.         return false;
  4.     }
it's working fine.
but i want to use the same expression in javascript also for client side validation

can any one tell me how i can use the same regular expression in javascript.
i am trying with following but it's not working
Expand|Select|Wrap|Line Numbers
  1.     var regex=/\(?=.*\d\)\(?=.*[a-z]\).{6,8}/;
  2.     if(regex.test(pword))
  3.     {
  4.         alert("valid password");
  5.     }
  6.         else
  7.         {
  8.                   alert("invalid password");
  9.          }
  10.  
Dec 1 '08 #3
kummu4help
14 New Member
Hi
i have the following regex in php and it's working fine for me
Expand|Select|Wrap|Line Numbers
  1. if(false===(preg_match("/^(?=.*\d)(?=.*[a-z]).{6,8}$/", $sInput) > 0))
  2. {
  3. return false;
  4. }
but i want to use the same regex in javascript for the sake of client side validation
can anyone tell how i can use the same regex in java script
the regex should test the following in a password
1. password should contain atleast one numeric and one alphabet
2.password should be in between 6 to 8 characters
i am trying with the following but it's not working.
Expand|Select|Wrap|Line Numbers
  1. var regex=/\(?=.*\d\)\(?=.*[a-z]\).{6,8}/;
  2. if(regex.test(pword))
  3. {
  4. alert("valid");
  5. }
  6. else
  7. {
  8. alert("invalid");
  9. }
Dec 1 '08 #4
anijos
52 New Member
try this

var regex = /(?=.*\d)(?=.*[a-z]).{6,8}/;
Dec 1 '08 #5
Markus
6,050 Recognized Expert Expert
First of all, kummu4help, we do not just happily 'hand out' code to anyone who asks for it. You have to show effort and understanding for us to help you.

Secondly, this is the PHP forum. If you're having problems with javascript, then you should go to javascript and ask.

Moderator.
Dec 1 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
JavaScript doesn't support atomic grouping - see Regular Expressions JavaScript.

So you'll have to try to find a combination that could work in one go or use two separate tests.
Dec 1 '08 #7
kummu4help
14 New Member
Hi acoder,
the following regex was working fine for me in javascript.
i forgot to place the $ sign at the end

var regex=/\(?=.*\d\)\(?=.*[a-z]\).{6,8}$/;
if(regex.test(pword))
{
alert("valid");
}
else
{
alert("invalid");
}
now i want to improve this so that it can work for caseinsensitive also.
but i don't know where to put the i (case insensitive) in the above regex
can u help me please....
Dec 2 '08 #8
acoder
16,027 Recognized Expert Moderator MVP
You also need an opening caret ^ at the beginning. For case-insensitive matches, use the i modifier, e.g. /^[a-z]+/i
Dec 2 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
Merged threads since they're on the same topic.

Moderator.
Dec 2 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
0
by: Lim Eng Teik | last post by:
I was asked by a client to make changes for their Outlook Web Access page where I need to validate Expiry Date of the Password and also the Password Length for the NT Account Policy. Initially I...
1
by: brijesh | last post by:
i am relatively new to javascript i am trying for a password validation code but iam not able to stop the page from loading the page when the password is wrong. the code is as below...
7
by: Mike | last post by:
I've been trying for the past week to put a simple code together. I have done a LOT of searching, found scripts showing the functions I would like to use, however when I mix them it all goes wrong,...
4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
6
by: Vishant | last post by:
Hi, I'm new to javascript and regEx and trying to solve the following problem. I have a function which validates the password if there is a number:...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
3
by: P2000 | last post by:
I want to make a regex for the replace function that will replace any "a" with "A" and any "b" with "B". This is what I did so far... (I'm trying to learn this RegEx thing) <form> <input...
1
by: shwethatj | last post by:
My problem is lik this , I am trying to create a registration form using javascript for a HR website , but i dont know how to provide password validation i.e the password and confirmation password...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.