Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Using RegEx to only allow one lowercase letter?

    I'm still a bit of a noob @ using regex so I can't work it out.... tried the below coddes but they both had problems..

    PHP Code:
    if (preg_match('/[^a-z](1)/'$char)) {
    die(
    'Invalid Request');

    That still allowed numbers of what I tested....

    PHP Code:
    if (preg_match('/[^a-z(1)]/'$char)) {
    die(
    'Invalid Request');

    That didn't allow numbers; but did allow more than one letter.

    Any help?

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    not exactly sure what you're trying to do here. Do you mean that the string can contain letters and numbers but only 1 of those characters can be a lowercase letter?
    If so... this should do the trick

    PHP Code:
     if (preg_match('/[a-z]{2,}/'$char)) {
    die(
    'Invalid Request');

    or do you mean you only want 1 character and it must be lowercase? (are numbers allowed?)
    PHP Code:
     if (!preg_match('/^[a-z]$/'$char)) {
    die(
    'Invalid Request');

    or if it can be a lowercase letter OR a number
    PHP Code:
     if (!preg_match('/^[a-z0-9]$/'$char)) {
    die(
    'Invalid Request');

    Last edited by NancyJ; 09-21-2005 at 02:40 PM.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks Nancy.. this would be the one..

    PHP Code:
     if (!preg_match('/^[a-z]$/'$char)) { 
    die(
    'Invalid Request'); 

    As for this code..

    PHP Code:
     if (preg_match('/[a-z]{2,}/'$char)) { 
    die(
    'Invalid Request'); 

    Yeah.. that's what I was trying to do with mine; but with just the number 1 in there.... guess I didn't look at the tutorial I have been reading close enough to notice they were curly braces & not parentheses.

    Cheers!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •