Jump to content

Regexp w/ Preg_Match


Jumpy09

Recommended Posts

EDIT!  I just figured out that RegExp has it's own Forum.  I apologize!

 

I have decided it may be quicker to seek help with this particular problem.

 

I really, really, really hate regexp with a passion.  It's one thing for JS, another for PHP, and for some reason I just can't seem to get them set up to do what I want properly.  So I'm just going to leave it up to people who probably have more experience with them or just all around smarter than I am.

 

Here is a list of RegExp that I am using and what it is "SUPPOSED" to do.

 

<?php

// Check username to make sure it only has Letters and Numbers
preg_match("/^[-a-zA-Z0-9]+$/", $uname);

// Check Password to make sure it has 2 Numbers
preg_match("/(.*[0-9].*[0-9])/", $pass);

// Check Password to make sure it has 2 Special Chars from the List
preg_match("/(.*[!,@,#,$,%,^,&,*,?,-,_,~].*[!,@,#,$,%,^,&,*,?,-,_,~])/", $pass);

// Check if Email is in Valid Format
preg_match("/^(?i)[0-9a-z]([-.\\w]*[0-9a-z])*@([0-9a-z][-\\w]*[0-9a-z]\\.)+[a-z]{2,9}\$/", $email);

//Check to make sure First Name has only letters, hyphens, or an apostrophes!
preg_match("/^(?i)[-a-z']$/", $fname);

// Check to make sure Last Name has only letters, hyphens, or apostrophes!
preg_match("/^(?i)[-a-z']$/", $lname);

?>

 

Now each and every single one of these features works properly in Javascript, unfortunately I can't quite figure out how to get them to work properly in PHP.

 

I use ( ! ) to check if there isn't a match, but the RegExp is giving me a bit of a headache.  What should be valid comes back as invalid.  I can't remember which I actually did, and which I just copied or found somewhere else.  I just want the torture to stop!  :)  Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/235420-regexp-w-preg_match/
Share on other sites

Alright the Username one works, I actually had a special character in it to test the error message.  Kind of forgot that.

 

However I think I may know why the fname and lname ones are not working correctly.  An apostrophe isn't required, as it is optional.  But I am looking for it anyway.  I'll look into that one, but the numbers and the special chars in the password should work correctly.

Link to comment
https://forums.phpfreaks.com/topic/235420-regexp-w-preg_match/#findComment-1209849
Share on other sites

I really don't understand why this isn't working correctly.

 

This is how they are in Javascript!

 

<script type="text/javascript">
      match(/(.*[0-9].*[0-9])/);
      match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/);
      match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
      /^[-A-Za-z0-9'\_]+$/i.test(required);
</script>

 

They work flawlessly, but even those identically in PHP doesn't want to do right.  Each time I load up the page and see the exact same errors, I want to literately punch my monitor.

 

If I take the ( ! ) out of the if statement, even a valid bit fails to produce the error.  So it's not matching up correctly.  Every change I make fails to produce a good result.

Link to comment
https://forums.phpfreaks.com/topic/235420-regexp-w-preg_match/#findComment-1209901
Share on other sites

/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/

You know that all those commas are not separators between characters, right? Your expression allows for !@#$%^&*?_~ and commas, and as such you really don't need all of them.

/(.*[!@#$%^&*?_~,].*[!@#$%^&*?_~,])/

(Of course, if you don't want to allow commas then remove them.)

Link to comment
https://forums.phpfreaks.com/topic/235420-regexp-w-preg_match/#findComment-1209938
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.