Core PHP, How To, PHP Beginners

Allow Only String and Numbers Using PHP

To Allow Only String and Numbers Using PHP, we use PHP in-built functions. We do it with textbox input. Where the user...

Written by Bikash · 1 min read >

To Allow Only String and Numbers Using PHP, we use PHP in-built functions. We do it with textbox input. Where the user gives some input on the textbox then using PHP inbuilt function we validate the staring or number using PHP.

Allow Only String and Numbers Using PHP
Allow Only String and Numbers Using PHP

Here are 2 thing we discuss about,

  • Allow only characters in textbox using PHP.
  • Allow only numbers in textbox using PHP.

Allow only characters in textbox using PHP

ctype_alpha: ctype_alpha() is PHP inbuilt function that can validate the user input is a string or any character or not. This also takes the spaces between characters as non-character.

Example:

Here we use PHP session to maintain the textbox value after page reload.

Output:

Allow only characters in textbox using PHP
Allow only characters in textbox using PHP

Allow only numbers in textbox using PHP

ctype_digit: ctype_digit() is also a PHP inbuilt function which validates only digit or numbers entered by the user. This is also doesn’t take any spaces on the textbox.

Example:

On the above code we also manage the value after page is reloaded by using PHP session.

Output:

Allow only numbers in textbox using PHP
Allow only numbers in textbox using PHP

How to Test

Copy complete code and create a PHP file then paste the code.

You can also use localhost to test. You can check the out also on above.

If you want to know more about those function, you can check on PHP official site here: https://www.php.net/manual/en/ref.ctype.php

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
Written by Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP. Profile

3 Replies to “Allow Only String and Numbers Using PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *