9.7 C
Thursday, March 30, 2023

Special characters not allowed in textbox in PHP

Must read

In this article, you going to see how to Special characters not allowed in textbox in PHP using simple example with a step by step.

If you are looking for this topic then you are at right place.

Let’s start topic regarding Special characters not allowed in textbox in PHP

Declare the form consist of input field type text with name as firstname.

Give label name to it.

Add submit button as input field with value as submit and named as submit.

On submit form post value for all the input field.

Remove the special characters from input field post value.

To remove special characters use function called preg_match function.

It check the pattern pass as parameter and returns the match found in the string.

Syntax for function is as follow as.

preg_match(pattern, input, matches, flags, offset)

Detail Information regarding syntax given below.

patternIt is Required. Consist of a regular expression indicating what to search for
inputIt is Required. String in which to be the search.
matchesIt is Optional. It is used in this parameter will be populated with an array containing all of the matches that were found
flagsIt is Optional.
offsetIt is Optional. Defaults to 0. Represents how much into the string to be searched.
Parameter Information for preg match

Thus use function preg_match to remove special characters on post form value.

Return false as an error on post.

Return true for not contain special character.

Below is code snippet from which you can understand every thing.

    if(preg_match("/[^a-zA-Z0-9]+/", $firstname))
    echo 'Invalid name contains special characters';
    echo $firstname;
<!doctype html>
<html lang="en">
  <title>Special characters not allowed in textbox in PHP</title>
<div style="background-color:#e5bcbc;" align="center">
<h2>Special characters not allowed in textbox in PHP</h2>
<form action="" method="post">
<p>Name: <input type="text" name="firstname" id="firstname" value=""  autocomplete="off"></p>
<input type="submit" value="Submit" name="submit">



Special characters not allowed
Special characters not allowed

Textbox textchanged event in PHP

Above image give the output for form post for the field name on clicking the submit button

Conclusion :

Finally, done regarding topic Special characters not allowed in textbox in PHP

I hope you like this article and if you feel like we missed out on anything, please comment below.

- Advertisement -spot_img

More articles


Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest article