Jump to content

Evaluate a string


daddywifi

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.
Link to comment
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
Link to comment
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
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.