Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 638
Vpp Man Avatar asked Sep 06 '25 06:09

Vpp Man


1 Answers

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.

like image 133
Pointy Avatar answered Sep 08 '25 22:09

Pointy