Jump to content

checking if string contains upper and lower case characters


Destramic

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.