-1

Hey I am looking for a string validator for PHP that will work with this code:

if(!isValidName($name))
{
    $errors[] = lang("MAIL_NAME_ERROR");
}   

That is the code that I have set up now I have tried this 2 codes and they do not return the value outputted here is the codes I tried:

//Checks if an name is valid
function isValidName($name)
{
   if (filter_var($name, MAIL_NAME_ERROR)) {
      return true;
   }
   else {
      return false;
   }
}

That is one that I tried it inputs the error message but does not return the value or reset the value another one that I tried that does not work is this :

function isValidName($name)
{
   if (strcspn($name, '0123456789') != strlen($name))
      return FALSE;
   $name = str_replace(" ", "", $name);
   $result = explode("_", $name);
   if(count($result) == 2)
      return TRUE;
   else
      return FALSE;
}

Basically what I am trying to do is when I don't input my name on my email form I am suppose to get an error message like so :

Please enter your full name

And when I do the message is suppose to go away. Well I get the message alright when the fields are empty but when there are any other values missing I get all the messages back like this :

Please enter your full name
Please enter your telephone number
Please enter your password
Failed security question

Does anybody know a string validator that will work with my string or set up another string?

jh314
  • 27,144
  • 16
  • 62
  • 82
  • 1
    What kind of filter is `MAIL_NAME_ERROR`?? – PeeHaa Jul 11 '13 at 18:06
  • You are going to need to clarify what you are trying to do. Are you just looking if the field is filled in (not likely)? Are you trying to see if the name only contains letters? – Derek Jul 11 '13 at 18:07
  • Downvoted you because this is the second time you are pointed to the invalid filter type: http://stackoverflow.com/questions/17583429/how-can-i-make-my-email-code-work-php#comment25586860_17583429 – PeeHaa Jul 11 '13 at 18:21

1 Answers1

0

If you just need to verify whether an input was set or not simply do the following:

if(isset($_POST["name"]) && !empty($_POST["name")){
// Name was entered
}
else{
// Name was not entered
}

As PeeHaa suggested, you can use filter_var($email_a, FILTER_VALIDATE_EMAIL) which is a built in PHP function that can validate different things, in this case emails. For more details check the doc

You can also take it a bit further and use this method: Using MX records to validate email addresses

Community
  • 1
  • 1
Dany Khalife
  • 1,850
  • 3
  • 20
  • 47
  • "For email and addresses i use Regular Expressions and preg_match" uhhhmm just no, unless you are really *really* good with regex: http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863 – PeeHaa Jul 11 '13 at 18:07
  • well sometimes you don't have a choice since filter_var is for PHP >= 5.2, but ill edit my answer to suggest filter_var too – Dany Khalife Jul 11 '13 at 18:09
  • Yes that is why I provided the regex in my answer which I doubt you are using. Nor do I suspect OP will be able to write that just by giving him a link to `preg_match` man pages and `www.regular-expressions.info` – PeeHaa Jul 11 '13 at 18:11
  • are you saying we should give the OP a drop in solution? – Dany Khalife Jul 11 '13 at 18:15
  • 1
    No I am saying that if you are going to answer at least make it helpful instead of giving OP the wrong path to take towards his/her solution. BTW I am all for *not* giving people on here a copy pasta solution. FWIW :-) – PeeHaa Jul 11 '13 at 18:16
  • ok ill see how i can make it more helpful, and yes if the OP shows more effort i don't mind either :) – Dany Khalife Jul 11 '13 at 18:22
  • 1
    Agreed on lack of effort from OP. Just found out there was already someone pointing him out a flaw in his code (see my comment under OP). – PeeHaa Jul 11 '13 at 18:23
  • Well I am still looking for the code and Dany where Do I input that code above as its not valide – user2555791 Jul 11 '13 at 18:47