Jump to content

Recommended Posts

Well, i hope this is ending up in the right place, if not i'm sorry.

 

First of, i'm still not that familiar with php as i hope to be.

I have a a slight problem with a registration code that i need help with.

 

First.

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Fel: Ogiltig E-mail adress";
}

if( empty($errors))

This code works just fine. But i i want to remove the if( empty($errors)) line. How would the code look like then?

I've tried several solution but non of them works.

 

Second.

I have to add a new code similar to the $email_address code named something like $med_nr, but the entering option would look something like this

S18757/2002

There's always a S in the beginning, 5 numbers then a / and then 4 numbers again.

 

Any help with this is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/284403-if-preg_match/
Share on other sites

RegExp for the 2nd question would look like this...

 

#S[\d]{5}/[\d]{4}#

 

As for the RegExp you posted, you can use commonly used shortcuts, like [\w] instead of [a-zA-Z_-], I think that'd help you somewhat.

 

Edit: Forgot a backslash.

Edited by Irate
Link to comment
https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1460762
Share on other sites

  • 2 months later...

[] is a character class and will match any one thing in it. So doing something like this or [\d] is superfluous; you'd just put S or \d (or whatever) without the [] surrounding it. In fact, \d itself is a character class. It's shorthand for [0-9]. So to match for S or an E, you would have [sE]. But since you want to match an S optionally followed by an E, then you'd do SE?.

 

Also, you're matching for a forward-slash but you're also using that as the pattern delimiter. So you have to escape the forward-slash in your pattern or else use a different pattern delimiter. I personally like using ~ since it rarely ever comes up, and / is pretty common because php is used with web stuff so matching paths and (x)html is common and / is a common symbol in them. # is also a popular alternative.

 

So overall, you should just have

 

~^SE?\d{5}/\d{4}$~

 

As to your first question:

First.

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Fel: Ogiltig E-mail adress";
}

if( empty($errors))
This code works just fine. But i i want to remove the if( empty($errors)) line. How would the code look like then?

I've tried several solution but non of them works.

 

I'm going to assume that you tried just removing if( empty($errors)) and that "didn't work". Well that line isn't directly part of your email regex condition, so I can't tell you why it "didn't work" or how to remove it in a way that "works". You're going to have to explain what you mean by "didn't work". What is (not) happening that you (don't) want to happen?

Link to comment
https://forums.phpfreaks.com/topic/284403-if-preg_match/#findComment-1470416
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.