Jump to content

Recommended Posts

hi all!

 

I have search on google on how to enter in a test box only caracters A-Z,a-z,0-9 using php, but all the solutions are not for me :(

I need, if you can help me to allow a user to insert only those caracters and with no space at all, like in username field.

 

Can you please help me with an example?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/177943-text-box-alphanumeric-caracters-only/
Share on other sites

If you want to prevent the user from actually entering the other characters then you need to use JavaScript. But, you will still want a PHP implementation to handle it in case someone has JS turned off.

 

For the PHP implementation you can use ctype_alnum(): http://us2.php.net/manual/en/function.ctype-alnum.php

i have found this:

 

<?php

$strings = array('AbCd1zyZ9', 'foo!#$bar');

foreach ($strings as $testcase) {

  if (ctype_alnum($testcase)) {

      echo "The string $testcase consists of all letters or digits.\n";

  } else {

      echo "The string $testcase does not consist of all letters or digits.\n";

  }

}

?>

 

 

 

 

I asume that will be in the top of my php page, but what to put on imput form???

 

<input type="text" name="name" size="40" />

 

 

 

Can you please give me a small example??? Thanks!

This post needs to be moved to javascript. robert.access, it appears you are misunderstanding how PHP works. PHP is a server side programming language. This means that once the page is sent to the viewer, you can no longer control it. That is where javascript comes in. You need to ask this question in javascript. If you are looking for a verification in PHP after the user has submitted the form. You should use a line like this:

if (preg_match("@^[A-Z0-9]+$@i", $_POST['myField'])) {

  // Do this

}

else {

// Fail string does not match.

}

This post needs to be moved to javascript. robert.access, it appears you are misunderstanding how PHP works. PHP is a server side programming language. This means that once the page is sent to the viewer, you can no longer control it. That is where javascript comes in. You need to ask this question in javascript. If you are looking for a verification in PHP after the user has submitted the form. You should use a line like this:

if (preg_match("@^[A-Z0-9]+$@i", $_POST['myField'])) {

  // Do this

}

else {

// Fail string does not match.

}

 

Teynon, did you even read the thread before posting? In my first response I already explained the difference between JavaScript validation and PHP validation, AND I provided a more efficient solution for PHP validation than using regular expression.

 

In my last post I asked for clarification on what he wanted to determine if it was JS or PHP he was asking about and instructed him to create a new thread in JS if that is what he needed. No need to move this thread since only PHP solutions have been dicussed.

hi all ant thanks for help.

sorry for long time that I dont read this post, but trying to find a solution.

 

So, like mjdamato says, if can I use php it's more convenient. I know verry little about javascript and php.

 

What I need it's

 

<form action....><imput type="text" name="x"></form>

 

so, in the form the user to not be able to copy paste, to not be able to enter !@#$%^&*()_+={}[];':",./<>? etc, just only alpha and numbers.

 

And also, into another form I want to limit the numbers they enter like:

<imput type=text  maxlength="1"> so user can imput from 0 to 9 but how can I limit this to only 5 from 0 to 5??

 

Thank you for your help.

 

 

An example on how to implement this it will verry verry usefull to figure out how to implement to my site.

 

Thank you very much.

If you want to prevent the user from actually entering the other characters then you need to use JavaScript. But, you will still want a PHP implementation to handle it in case someone has JS turned off.

 

For the PHP implementation you can use ctype_alnum(): http://us2.php.net/manual/en/function.ctype-alnum.php

 

As mjdamato already stated if you don't want them to be able to enter the characters you will have to use JavaScript. PHP has no control over the users browser.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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