Jump to content


Photo

Evaluate a string


  • Please log in to reply
3 replies to this topic

#1 daddywifi

daddywifi
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 August 2006 - 08:41 PM

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.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 31 August 2006 - 08:47 PM

What do you mean by "1 of 2 diffrent values"? For example- they can be only "A" or "B"?

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 31 August 2006 - 08:52 PM

You could use a regular expression:

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

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'

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

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 daddywifi

daddywifi
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 September 2006 - 05:25 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users