Vpp Man
Vpp Man

Reputation: 2546

Allow only lowercase characters

I use following code to check if a user input is lowercase or not. I will allow characters from a to z. no other characters allowed.

JavaScript file:

var pat = /[a-z]/;

function checkname()
{
  var t = $("input[name='user_name']").val();

  if(pat.test(t) == false)
  {
    alert('Only lowercase characters allowed');
  }
}
//... other functions

But this donot work all the time. If I enter industrialS, it will not find that capital 'S'.

I also tried: /^[a-z]$/ and /[a-z]+/. But not working.

PLease help me.

Upvotes: 4

Views: 19996

Answers (5)

Robert Puhan
Robert Puhan

Reputation: 11

Here is the solution that worked for me: Add md5 before password like so:

$password=md5($_POST['password']);

In the database, select users and edit password then change it to the MD5 longtext password.

That way your password will be case sensitive.

Upvotes: 0

0lukasz0
0lukasz0

Reputation: 3267

Your regexp should be:

/^[a-z]+$/

Upvotes: 0

Ananta Prasad
Ananta Prasad

Reputation: 3859

if((/[a-z]/.test(email))==true){//allow the small characters}

Upvotes: 0

Jakob Jingleheimer
Jakob Jingleheimer

Reputation: 31580

Since all you want is lower case letters, instead of just telling the user s/he's done something wrong, I would fix it:

function checkname() {
    var disallowed = /[^a-z]/gi; // g=global , i=case-insensitive
    if (this.value == disallowed) {
        //delete disallowed characters
        this.value = this.value.replace(disallowed,'');
        alert('Only lowercase letters allowed');
        //instead of an alert, i would use a less intrusive fadeIn() message
    }
    this.value = this.value.toLowerCase();
}

Upvotes: -1

Pointy
Pointy

Reputation: 413702

Your regular expression just checks to see if the string has any lower-case characters. Try this:

var pat = /^[a-z]+$/;

That pattern will only match strings that have one or more lower-case alphabetic characters, and no other characters. The "^" at the beginning and the "$" at the end are anchors that match the beginning and end of the tested string.

Upvotes: 12

Related Questions