Destramic Posted April 12, 2011 Share Posted April 12, 2011 i have a script that checks if a sting has numbers how can i check if the string has upper and lower case characters please? function string_has_numbers(string) { var regex = /\d/g; return regex.test(string); } Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/ Share on other sites More sharing options...
Adam Posted April 12, 2011 Share Posted April 12, 2011 Do you want it to match for only numbers, upper-case and lower-case letters, or make sure the string contains at least one of each? Or even a string that only consists of numbers, upper-case and lower-case letters, and has to have one of each? Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/#findComment-1200598 Share on other sites More sharing options...
Destramic Posted April 12, 2011 Author Share Posted April 12, 2011 well i'd like another function which just checks if the string has uppper and lower case please Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/#findComment-1200602 Share on other sites More sharing options...
Adam Posted April 12, 2011 Share Posted April 12, 2011 I'll leave the naming of it to you... function nameHere(str) { return str.match(/[a-z]/) && str.match(/[A-Z]/); } This would be more apropriately added as a method to the String object by the way. That way instead of passing it as a parameter to the stand-alone function, you could simply call it like myString.nameHere() - which would be done with the following code: String.prototype.nameHere = function() { return this.match(/[a-z]/) && this.match(/[A-Z]/); } But which you use it up to you of course. Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/#findComment-1200612 Share on other sites More sharing options...
Destramic Posted April 12, 2011 Author Share Posted April 12, 2011 thank you...although i could get it to work this way String.prototype.string_has_numbers = function(string) { var regex = /\d/g; return regex.test(string); } var password_value = "test1"; password_value.string_has_numbers() Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/#findComment-1200614 Share on other sites More sharing options...
Adam Posted April 12, 2011 Share Posted April 12, 2011 When you extend the object you don't pass the string in as a parameter, it's already available as this: String.prototype.string_has_numbers = function() { return this.match(/\d/); } You don't need the 'g' modifier in that expression by the way. I'd consider renaming that method too, to be more in-line with the other String methods, like .indexOf(), .charAt(), etc. Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/#findComment-1200616 Share on other sites More sharing options...
Destramic Posted April 12, 2011 Author Share Posted April 12, 2011 thanks for your help now ive learnt something too...plus my password strength function is complete now...thanks again Link to comment https://forums.phpfreaks.com/topic/233489-checking-if-string-contains-upper-and-lower-case-characters/#findComment-1200622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.