Jump to content

form validation


vang163

Recommended Posts

hi,

 

i'm currently stuck in validating some textfields in a html form using php.

I want to make the textfield to accept only characters that are pre-defined.

 

eg. textfield to accept only these defined alphabets/numbers/special characters: a-z,A-Z,0-9,!@#$%^&*()[]/\{}<>,.?;:"'

 

I've searched the web for solutions but no results, maybe i search with a wrong topic.

 

Can you show me how this can be done? your help is greatly appreciated, thanks.

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/
Share on other sites

use a regular expression on the field input to match against a pattern full of the chars you want to allow.

 

$reg_exp = '/^[A-Za-z0-9\,\!\@\#\$\%\^&\*\(\)\[\]\/\\\{\}\<\>\,\.\?\;\:\"\']{,}$/'

if(preg_match($reg_exp, $field_text)
{
     echo "YAY we found a match!";
}
else
{
     echo "Try again!";
}

 

I do think that would do the trick for you. And you can find some great Regular Expression information at:

 

http://www.php.net/manual/en/book.pcre.php

http://www.regular-expressions.info/php.html

 

Hope this helps!

-Ron

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/#findComment-582145
Share on other sites

if you want to screen the users' input from the client-side, meaning allowing the users to only type those characters you mentioned then you need to use JavaScript for that instead of PHP. I have been using that one also.

 

But still you need to screen that one the second time around it goes to the server, just in case those were bypassed. To do that, you need to use preg_match() to check if there are invalid characters for that and thus, you will use regex for that.

 

my two cents,

 

Jay

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/#findComment-582146
Share on other sites

hi,

 

i've modify your code like below:

 

$regular_expression = '/^[A-Za-z0-9]/';    // to allow only A-Z, a-z, 0-9

 

if(!preg_match($regular_expression, $text_field_name))

{ $errors[] = 'Please remove special characters in the text field';

}

 

when i enter the following data into the textfield:

input 1:  !@#password

result: everything works fine, display error message

input 2: password!@#

result: error! it does not catch the special characters in the textfield and no display of error message

 

may i know where i have done wrong?

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/#findComment-582156
Share on other sites

<?php
$reg_exp = '/^[A-Za-z0-9\,\!\@\#\$\%\^&\*\(\)\[\]\/\\\{\}\<\>\,\.\?\;\:\"\']{,}$/';
?>

 

Note the ^ and & in the expression

 

the ^ will define the begin

 

the $ will define the end

 

so if you change your code to

 

<?php
$regular_expression = '/^[A-Za-z0-9]{,}$/'; 
?>

 

That should work for you.

 

-Ron

 

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/#findComment-582159
Share on other sites

hi ron,

 

many thanks for your guidance. i've again modify your code like below:

 

$regular_expression = '/^[A-Za-z0-9]{,}$/';    // to allow only A-Z, a-z, 0-9 in text field

 

if(!preg_match($regular_expression, $text_field_name))

{  $errors[] = 'Please remove special characters in the text field';

}

 

But now all my acceptable data i enter into the textfield will give me this error message, except empty field will not give me this error message!

My acceptable data includes:

a. data in number

b. data in alphabet

c. data in alphabet and number

 

may i know where have gone wrong?

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/#findComment-582165
Share on other sites

Sorry about that it's the {,} i never used it empty like that and yes it does not work.

 

<?php

$reg_exp = '/^[a-zA-Z0-9 ]*$/';

?>
[\code]

notice the space in the [ ] and the *

the space in the [ ] will allow for a white space char if you do not want any white space, remove it and it will not match if there is a space char.

The * allows for 0 or more of the [ ] items.

Sorry for the crazyness, but I strongly suggest that you read up and have at least a basic understanding of Regular Expressions. They can be extreamly useful!

Link to comment
https://forums.phpfreaks.com/topic/113308-form-validation/#findComment-582179
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.