0

I've to put validation for password. Password must be of 8 characters (min = 8 char and max = 10 char). And should contain at least 1 uppercase character, 1 lowercase character, 1 special character and 1 number. Till now what I've done is below...

['validate-password', 'Password must contain at least 1 uppercase character, 1 lowercase character, 1 special character and 1 number (Min. length = 8 & Max. length = 10)', function(v) {
                var pass=v.strip(); 
                if(pass.length <11 && pass.length>7)
                    return Validation.get('IsEmpty').test(v) || /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/.test(v) || /^\s|\s$/.test(pass)

            }],

This code works fine, but when I add white space at the starting of password, e.g. password = 2 whitespaces + Aaaa#7 then it is showing alert message but when I add 2 more characters at the end like password = 2 whitespaces + Aaaa#7 + 12 then it is accepting. What I want here is removal of whitespace at the beginning and at the end of string... How can I do that???

2

3 Answers 3

1

If you are using jQuery, you can use

$.trim(str)

to trim whitespace on either side, otherwise you can use:

str.replace(/^\s+|\s+$/g, '');

to remove whitespace on either side.

Sign up to request clarification or add additional context in comments.

Comments

1

Use $.trim(str) remove whitespace at the beginning and at the end of string OR yourString.replace(/\s/g,''); to remove all of white spaces in your string.

Comments

0

Whitespace may containers \t,\r,\s.Here is the code in jQuery to trim whitespace, it works fine in any condition.

whitespace = "[\\x20\\t\\r\\n\\f]";
rtrim = new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" )

str.replace(rtrim , '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.