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???

Looking Forward
  • 3,579
  • 8
  • 45
  • 65
  • 3
    Use the `trim` function before validating the password. See http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript – Floris Dec 11 '13 at 04:37
  • 1
    duplicate of [How do I trim a string in JavaScript?](http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript) – Felix Kling Dec 11 '13 at 04:41

3 Answers3

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.

RoneRackal
  • 1,243
  • 2
  • 10
  • 16
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.

Ringo
  • 3,795
  • 3
  • 22
  • 37
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 , '');
yoyo
  • 722
  • 6
  • 4