944,162 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 941
  • PHP RSS
Aug 2nd, 2007
0

Having trouble with my form validation

Expand Post »
I'm having a trouble getting my php script to validate user input what am I doing wrong here? This just displays the error message all the time.

for example:
PHP Syntax (Toggle Plain Text)
  1. if(!preg_match("/[^a-zA-Z0-9\.\-¤œ¼\ ]+$/",$name)||empty($name))
  2. {
  3. print '<td colspan="2" class="ErrorText"><div align="right">Please enter a valid name without special characters i.e. *,/,&lt; etc. </div></td>';
  4. $error++;
  5. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wireshop is offline Offline
3 posts
since Aug 2007
Aug 3rd, 2007
0

Re: Having trouble with my form validation

I don't use preg_match, but this code works for me to make sure users only use numbers and letters.
PHP Syntax (Toggle Plain Text)
  1. if (eregi ("^[[:alnum:]]+$", $_POST['username'])) {
  2. $a = TRUE;
  3. } else {
  4. $a = FALSE;
  5. $message[] = "Please enter a username that consists only of letters and numbers.";
  6.  
  7. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nanharb is offline Offline
7 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Having trouble with my form validation

You can also use ctype_alnum, this way:

PHP Syntax (Toggle Plain Text)
  1. $username = ctype_alnum($_POST['username']) ? $_POST['username'] : NULL;
  2.  
  3. if($username == NULL) {
  4. echo 'error message';
  5. } else {
  6. echo $username;
  7. }
Last edited by cereal; Aug 3rd, 2007 at 9:13 am.
Reputation Points: 201
Solved Threads: 86
Posting Pro
cereal is offline Offline
524 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need information about Rand()
Next Thread in PHP Forum Timeline: prevent scraping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC