views:

39

answers:

1

Hey all,

Tried posting this before but it did not go through (i think) so if this is a repost please disregard. However, I asked a question yesterday and figured it out with the wonderful help from here. I have a user account page where a user can edit their account information. Now, they don't always change their password. So, I made the Current Password box required only when the new password box had an input. I also applied equalTo: so that I can check the confirm password box with the new password box. That is all working. However, now I want to apply some rules to the new password box. Like the password has to be at least 6 chars long, contain one letter and one number, and not contain other characters. Can somebody please point me in the right direction?

here is some of the code I have for the min length:

$(<%= NewPass1.GetName() %>").rules("add", {
  minlength: 6,
SOME REGEX HERE?
A: 

Try this function. It checks that the password is alphanumeric and at least 6 characters long. It also checks that it contains at least one digit and one letter.

function isValid(input)
{
     var reg = /^[^%\s]{6,}$/;
     var reg2 = /[a-zA-Z]/;
     var reg3 = /[0-9]/;
     return reg.test(input) && reg2.test(input) && reg3.test(input);
}

I assume you are using the Validation plugin. To use a custom method with the plugin, try this function: http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage

EDIT:

Edited to allow all characters except spaces and '%'.

SimpleCoder
what if i want to include symbols....every thing is legal besides spaces and the % symbol
Tom
Try my new edit
SimpleCoder
sounds good right now im just looking on how to use this function with the addmethod. this is my first go around with javascript and jquery
Tom
make sure you check out the Example subtab on the link I posted. There are a few examples on using addmethod. (Check the bottom of the page)
SimpleCoder
How is this for my addMethod? jquery.validator.addMethod("passwordRules", function(input){ var reg = /^[^%\s]{6,}$/; var reg2 = /[a-zA-Z]/; var reg3 = /[0-9]/; return reg.test(input) }
Tom
That should do it, but you forgot ); at the very end. Just add those two characters at the very end and it should work.
SimpleCoder
got that now im racking my brain...normally i would apply rules by doing required: the rule. but what would i use now? not required anymore? what if its within $("#aspnetForm").validate? or should I do an individual validation on the text box itself.
Tom
I would validate it manually.
SimpleCoder
http://stackoverflow.com/questions/3094733/jquery-add-method-and-implementation
Tom
I placed the link above to my other question...just so i can reformat the question in a proper format. also it has neater code.
Tom