Jump to content

Validate Field


affc

Recommended Posts

Hello all,

 

I am trying to do a php check to make sure the input of the field is actually alphanumeric for the NAME that also allows uppercase, and the username that cannot accept Uppercase, please does anyone knwo why this code is not working? Just processes it as complete everytime.

 

<?php
$NAME=$_POST['NAME'];
$Username=$_POST['Username'];

if (preg_match('/[^A-Za-z0-9]/', $NAME)){
header("Location:complete.php");
}
else{
header("Location:error.php");
}
if (preg_match('/[^a-z0-9]/', $Username)){
header("Location:complete.php");
}
else{
header("Location:error.php");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/118790-validate-field/
Share on other sites

You need to put exit; after your header("Location:") calls.  And you can't have an elseif where you have it...And you only want to redirect to complete.php if EVERYTHING checks out.  Oh yeah and you don't need a regex for this.  Just use ctype_alnum().

if (!ctype_alnum($NAME)) {
      $error = true;
}
if (!ctype_alnum($Username)) {
      $error = true;
}
if ($error) {
   header("Location: error.php");
}
else {
   header("Location: complete.php"); 
}

 

Just an idea.  Not necessarily how I'd do validation, but it gets the job done.

Link to comment
https://forums.phpfreaks.com/topic/118790-validate-field/#findComment-611614
Share on other sites

Just discovered that it actually just changes the field to all lowercase eg : FieLd - will change to - field. I need it to be able to produce the error if it is not lower case alpha numeric. As the user will then need to change the username to lowercase manually so they are aware when its processed its all in lowercase, can this be done as I cannot find a way to do it?

 

Thanx

Link to comment
https://forums.phpfreaks.com/topic/118790-validate-field/#findComment-617930
Share on other sites

Archived

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

×
×
  • 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.