Jump to content

Archived

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

Mateobus

PHP form validation

Recommended Posts

I am trying to write a php function that validates a username and password. To keep things simple i want to allow only letters and numbers upper and lower case allowed.  Is there a good function for this?

Share this post


Link to post
Share on other sites
If you want to define what characters to allow/disallow, look into regular expressions.

Share this post


Link to post
Share on other sites
This isn't perfect but...

if (preg_match('/^[0-9a-zA-Z]+$/i', $_POST['FieldName'])) {
  echo 'matches';
} else {
  echo 'no match';
}

Share this post


Link to post
Share on other sites
[quote author=gijew link=topic=102502.msg406858#msg406858 date=1154386656]
This isn't perfect but...

if (preg_match('/^[0-9a-zA-Z]+$/i', $_POST['FieldName'])) {
  echo 'matches';
} else {
  echo 'no match';
}
[/quote]
if you have the i modifier, you dont need the a-z bit in the expression gijew. So the expression can be just:
[code]/^[0-9A-Z]+$/i[/code]

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.