Jump to content

What am i doing wrong?


takn25

Recommended Posts

Hi this is my code I am just understanding Regex kinda a new at this.

I have a  string which is Tom I want to check that there is nothing other than Alphabets used I made the Pattern variable and used both in the preg_match function. The problem I am facing personally just looking at the code I think the echo should be Alphabets are used but for some reason I am getting echo  Contains something other than Alphabets. I haven't been able to figure out what am i doing wrong some help would be appreciated.

 

$pattern = "[^A-Za-z$]";

 

$tom="Tom";

 

if (preg_match($pattern, $tom))

{echo "Alphabets are used";} else {echo "Contains something other than Alphabets";}

Link to comment
Share on other sites

Your IF logic is backwards. Your regex is close. It needed some delimiters. Plus you should remove the $. Besides that it was looking to see if there are any NON alpha characters. However, that is how you want to do the validation. It is easier to look for any instance of a non-alpha character than to ensure all the characters are alpha characters. You just need to change the IF/ELSE logic.

 

$pattern = "#[^A-Z]#i";

$tom="Tod4sdadasm";

if (preg_match($pattern, $tom)) 
{
    echo "One or more non-alpha characters were found";
}
else
{
    echo "All the characters where alpha";
}

 

Also modified the regex by adding the "i" parameter to make the test case insensitive.

 

Change pattern to

$pattern = "/^[A-Za-z]/";

 

Um, no. That expression only tests to see if the FIRST character is a non a-z character.

Link to comment
Share on other sites

Hi thanks both of you for helping. mjdamato i just read your post that my  if else logic is backwards could you please tell me what is the proper way to achieve my desired result.

 

Didn't I post the code already?

 

Let me state again. You Regex had some minor problems but the way it was written it was supposedly checking to see if there were any NON-ALPHA characters. So, the statements in the IF and ELSE conditions were backwards. IF there was a match, then you want the message that there were non-alpha characters, ELSE the string only contained alpha characters.

 

As, I also stated that is exactly how you want to do that validation - look for any character OTHER THAN alpha characters rather than ensure that all the characters are alpha. It is basically the same check, but programatically it is different.

 

The code I provided should do what you want.

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.