Jump to content

Why does this matching fail?


vijdev

Recommended Posts

$input=hjkhk$#@$@;
$test=preg_match_all("/^[A-Za-z]{1,20}/",$input,$match);

 

why does it pass $#@$@, although my definition ensures only alphas?

how do i ensure $#@$@ is not passed, or anything else from the keyboard like :;{"}[]! is not passed?

please help!

Link to comment
Share on other sites

Better to do that check outside of regex:

 

$stringLen = strlen($string);
if ($stringLen < 1 || $stringLen > 20 || !preg_match("/[A-Za-z]*/",$input) ) {
    echo 'You have provided invalid input. Valid input ......'; 
}

 

Untested, pending any retarded errors I made should work for what you want.

Link to comment
Share on other sites

Better to do that check outside of regex:

 

$stringLen = strlen($string);
if ($stringLen < 1 || $stringLen > 20 || !preg_match("/[A-Za-z]*/",$input) ) {
    echo 'You have provided invalid input. Valid input ......'; 
}

 

Untested, pending any retarded errors I made should work for what you want.

 

thanks, but would this solve my initial problem of not passing $#@$@ ?

Link to comment
Share on other sites

What exactly do you want this regular expression to do? You want it to match alphabetic-characters at the beginning of a string? If so, it appears to be working fine for me:

<?php
$input='hjkhk$#@$@';
preg_match_all("/^[A-Za-z]{1,20}/",$input,$match);
print_r($match);
?>

 

produces:

Array
(
    [0] => Array
        (
            [0] => hjkhk
        )

)

Link to comment
Share on other sites

If I am not mistaken the {1,20} means 1 OR 20 not one through 20.

 

Chances are that is your issue.

 

Hmm I've always taught {1,20} meant the range of characters accepted, so in this case characters between 1-20 are allowed. But thanks for the correction.

Link to comment
Share on other sites

 

thanks, but would this solve my initial problem of not passing $#@$@ ?

 

Did you try it?

 

The reason it was returning true was because 1 or 20 had an alpha character, which was a valid response.

Link to comment
Share on other sites

Ok, Let me put again the problem definition for clarity:

 

1.I need to check if input is only alphabets, not one single other type character

2.Needs to be minimum 1 char and max 20 characters long

 

This is what am intending to do with above expression.Any directions?..have to admit it looks so simple! :|

Link to comment
Share on other sites

Unless I am terribly mistaken, {1,20} means anywhere between 1 and 20 (inclusive) occurances of the preceeding class. In this case, your RegExp will match any string that starts with 1 or more alphabetic characters (upper or lower case) regardless of how long the string is.  It will match this entire paragraph. 

 

I think if you anchor the RegExp to the end of the string as well, then it will do what you are expecting:

 

$input='hjkhk$#@$@';
$test=preg_match_all("/^[A-Za-z]{1,20}$/",$input,$match);

 

The caret ( ^ ) anchors to the start of the string and the dollar-sign ( $ ) anchors to the end. So now it should match a string of 1 to 20 alphabetic characters.

 

I am no RegExp expert, not even a little bit.  But I think this is correct. Give it a go

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.