Jump to content

Archived

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

daddywifi

Evaluate a string

Recommended Posts

I am still new to PHP and I am trying to create a page to evaluate a form field for the following things:

1. Not be empty, which I have successfully completed
2. To be a certain length, which I have also sucessfully completed
3. That the first 5 characters be 1 of 2 different values

Can anyone help me? Thank you.

Share this post


Link to post
Share on other sites
What do you mean by "1 of 2 diffrent values"? For example- they can be only "A" or "B"?

Orio.

Share this post


Link to post
Share on other sites
You could use a regular expression:

[code]
if (preg_match("/^(12345|54321)/", $_GET['field_name'])){
  //do something here
}
else {
  // ...
}
[/code]

Should do it.

In fact you could use a RegEx for the whole thing.  Lets say your field has to have 9 characters, not be null and start with either 'email' or 'phone'

[code]
if (preg_match("/^(email|phone)\w{4}/", $_GET['field_name'])){
  // It's fine...
}
else {
  // show me an error...
}
[/code]

Regards
Rich

Share this post


Link to post
Share on other sites
Orio,
Yes an "A" or "B" value. Also thanks HuggieBear, but I am unsure what all of the items in the expressions you provided mean :(

Here is a better ex of what I am doing:

I have some users that are identified by a unique 14 digit#, but the first 5 digits of the number meet 2 different values either value "A" or value "B". I provide a subscription service that they can access from home, but want to verify that they are my users before allowing access to the service. In doing this I need to be sure they dont enter a null value, that the value entered is 14 digits, and that it matches either value "A" or value "B". I had this created to evaluate the form field, but it returns false each time:

if (empty($id) || strlen($id) != 14 || substr($id,0,5) != '12345' || substr($id,0,5) != '09876')


Thanks again for the help

Share this post


Link to post
Share on other sites

×

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.