PHP- how to allow only letters in a text field. Also how to only allow numbers in a text field

html input dropdown
input type=text css
html input types
input type textarea
input type=number decimal
php remove html tags and content
html form controls
html drop-down list

How do I add a input field that only accepts letters. Also how do I add a input field that only accepts numbers. And once the user inputs say letters in a text field that only accepts numbers it gives an error.

if(empty($_POST['firstname']))
        {
            $errors['firstname1'] = " Required";
        }

    if(empty($_POST['zip']))
        {
            $errors['zip1'] = " Required";
        }

    <!-- Letters only for firstname -->

    <p>
                <label for="firstname" class="label"><font color="#040404">*First Name:</font></label>
                <input class="textinput" id="firstname" type="text" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname']; ?>" /><?php if(isset($errors['firstname1'])) echo $errors['firstname1']; ?>
            </p>
    <!-- Letters only for zip -->

    <p>

                    <label for="zip" class="label"><font color="#040404">*Zip Code:</font></label>
                    <input id="zip" type="text" name="zip" value="<?php if(isset($_POST['zip'])) echo $_POST['zip']; ?>" /><?php if(isset($errors['zip1'])) echo $errors['zip1']; ?> <?php if(isset($errors['zip2'])) echo $errors['zip2']; ?>
                </p>

Consider using the PHP functions ctype_alpha & ctype_digit in your form validation.

ctype_alpha returns true only if validation consists of letters, and will return false if numeric or special characters are used.

ctype_digit returns true only if validation consists of numbers, and will return false if alphabetic or special characters are used.

Here's a quick example that will check an array. It will return false since one of the strings has a number in it:

$string = array('word', 'number5');
if (ctype_digit($string)) {
    echo "All numbers are true!";
} else {
    echo "Something's not right";
}

Hope this helps!

HTML Input Types, <input type="radio"> defines a radio button. Radio buttons let a user select ONLY ONE of a limited number of choices: Example. <form> In may Addvalue.php file 3 fields id, name, color… in id only numbers allow, in name and color only letter is allowed. Values successfully inserted into my database, but the problem is when user write NAME and COLOR in the HTML FORM field only LETTERS allow to write, if write any NUMBER message appears

how to allow only letters in a text field.

You can use pattern attribute of HTML5 to allow only letters in a text field, like this:

<input type="text" name="fieldname1" pattern="[a-zA-Z]{1,}" required>

how to only allow numbers in a text field

Simillarly, make the type attribute number to only allow numbers in a text field, like this:

<input type="number" name="fieldname2" required>

Or, use the pattern attribute, like this:

<input type="text" name="fieldname2" pattern="[0-9]{1,}" required>

Sidenote: Even though these input fields on the client side will restrict users to some extent, they can't be reliable and your only line of defense. Use PHP Regex on the server side to strictly validate your input data.

strip_tags - Manual, HTML comments and PHP tags are also stripped. Tag names within the input HTML that are greater than 1023 bytes in length will be treated as htmlspecialchars() - Convert special characters to HTML entities small function I wrote to allow only specific tags with specific attributes and strip all other tags and attributes. Hello, as the title says i want to have on a form a "text" field to accept only numbers (0-9) is this possible? i hope someone could answer me fast, i need to finish it today o.0 Thanks! <input type="text" to accept numbers only, its possible?

You do not need to write any extra code to satisfy your requirement

Following is a example which allows only letters (both small and capital in any sequence).

<input id="my_id" type="text" pattern="[A-Za-z]*" class="validate">

Or you can give more constraints like allow only first letter to be capital and reaming in small caps with pattern="[A-Z][a-z]*"

To allow only numbers use pattern="[0-9]*" and to allow only fix number (say 10) of numbers use pattern="[0-9]{10}"

ctype_alnum - Manual, Checks if all of the characters in the provided string, text , are alphanumeric. a single character (negative values have 256 added in order to allow characters in the for alphabetic character(s); ctype_digit() - Check for numeric character(s) It is also important to note that the behavior of `ctype_alnum` differs according to​  In order to block anything but numbers from being input into a text field but still allowing for other buttons to work (such as delete, shift, tab, etc.) look at a reference of the Javascript key codes; anything from 65 on up (to 222) can be blocked.

For numbers only, here's a snippet I use with jQuery:

$('.content').keydown(function(e){
    var c = (e.charCode || e.keyCode);
    if(c<48 || c>57){
        return false;
    }else{
        return true;
    }
});

<input type="text">, input elements of type text create basic single-line text fields. maxlength, The maximum number of characters the input should accept. minlength Constraint validation is only applied when the value is changed by the user. The following non-standard attributes are also available on some browsers. Inside this function, we check if the character code of the input character lies between the standard ASCII character codes for alphabets. For reference, the capital A-Z start from 65 and ends at 90 and small a-z starts from 97 and ends at 122. Here is the code for allowing only alphabets on key press.

Note : This is for PHP only

If you Want to validate with server side with PHP you can use:

function validate_string_spaces_only($string) {
    if(preg_match("/^[\w ]+$]/", $string)) {
        return true;
    } else {
        return false;
    }
}

string consiting only of letters, numbers and optional spaces

How to force Input field to enter numbers only using JavaScript , Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the iput field that will popup  Textfield only accepts numbers. import java.awt.Container; import java.awt.Graphics; Make sure that my Text field has the focus when a JFrame is created: 40.

JavaScript : Checking for all numbers, Sometimes situations arise (input a phone number, zip code or credit input numeric characters only'); document.form1.text1.focus(); return  5. Create a method name Numvalidator that has the parameter of JTextField with txtField variable. This will filter all those characters that are not numbers in the textfield. And will prompt "Only numbers are allowed!".

How to allow only alphabets in input field in JavaScript?, In this article, we will learn how to allow only alphabets in input field in JavaScript​? <form action="/data.php" method="get"> <input type="text"  Voted Best Answer. For exact number of digits you can use this script as the custom validation: For a number between X and Y digits, use this: Of course, I'm assuming here that you've set the field as a Number field, so it will only allow digits to be entered.

Restrict keyboard character input with jQuery, In this tutorial, I show how you can restrict keyboard character input with jQuery and JavaScript. Allowing only number Below script allows only number input. 97-122 - (a-z) 8 - (backspace) 32 - (space) */ // Not allow special if ( !( You can also use it to restrict some of the characters or perform an  make sure you add doc type html5 in your html markup and try this in your form <input type="number" name="phone" min="10" max="12&quot; /&gt; &lt;input type=&quot;tel

Comments
  • Are you considering javascript as an option?
  • To the best of my knowledge, you can't do this with just PHP and HTML. You could use Javascript to 'enforce' your rules, though.
  • HTML5 patterns in the input boxes?
  • <input type="text" name="firstname" pattern="[A-Za-z]{3}" title="At least three letters please"> --- this allows at least 3 letters, <input type="text" name="zip" pattern="[0-9]{3}" title="At least three zip numbers please"> --- this allows at least 3 numbers.
  • this helped a lot my man. Also for a text field how do I only allow numbers and letters and no special characters. Its for an address field
  • @ShivPatel Make the pattern attribute like this, pattern="[a-zA-Z0-9]{1,}"
  • @Downvoter, Would you please tell me the reason of this downvote? May be I can learn a thing or two from this.
  • PHP tag makes no sense?
  • Well, validating input at the server only is a bit late, it is preferable to prevent unwanted input client-side. Otherwise, you'll have to validate in PHP, send and error message back to the client, then wait again for another input. Client-side control prevents unnecessary back-and-forth between client and server.
  • Yes, but you must not forget the validation server-side. Because the javascript can be manipulated from client, and will be vulnerable to attacks