Hướng dẫn number validation in php

An HTML form contains various input fields such as text box, checkbox, radio buttons, submit button, and checklist, etc. These input fields need to be validated, which ensures that the user has entered information in all the required fields and also validates that the information provided by the user is valid and correct.

Nội dung chính

  • Empty String
  • Validate String
  • Validate Number
  • Validate Email
  • Input Length Validation
  • Validate URL
  • Button Click Validate
  • Create and validate a Registration form
  • What is form validation in PHP?
  • How do I validate a number?
  • How will you create forms and validate the form input in PHP?
  • How many types of validation are there in PHP?

There is no guarantee that the information provided by the user is always correct. PHP validates the data at the server-side, which is submitted by HTML form. You need to validate a few things:

  1. Empty String
  2. Validate String
  3. Validate Numbers
  4. Validate Email
  5. Validate URL
  6. Input length

Empty String

The code below checks that the field is not empty. If the user leaves the required field empty, it will show an error message. Put these lines of code to validate the required field.

Validate String

The code below checks that the field will contain only alphabets and whitespace, for example - name. If the name field does not receive valid input from the user, then it will show an error message:

Validate Number

The below code validates that the field will only contain a numeric value. For example - Mobile no. If the Mobile no field does not receive numeric data from the user, the code will display an error message:

Validate Email

A valid email must contain @ and . symbols. PHP provides various methods to validate the email address. Here, we will use regular expressions to validate the email address.

The below code validates the email address provided by the user through HTML form. If the field does not contain a valid email address, then the code will display an error message:

Input Length Validation

The input length validation restricts the user to provide the value between the specified range, for Example - Mobile Number. A valid mobile number must have 10 digits.

The given code will help you to apply the length validation on user input:

Validate URL

The below code validates the URL of website provided by the user via HTML form. If the field does not contain a valid URL, the code will display an error message, i.e., "URL is not valid".

Button Click Validate

The below code validates that the user click on submit button and send the form data to the server one of the following method - get or post.

Note: Remember that validation and verification both are different from each other.

Now we will apply all these validations to an HTML form to validate the fields. Thereby you can learn in detail how these codes will be used to validation form.

Create a registration form using HTML and perform server-side validation. Follow the below instructions as given:

Create and validate a Registration form

When the above code runs on a browser, the output will be like the screenshot below:

Fill the registration form and click on the Submit button. If all required information is provided correctly, the output will be displayed on the same page below the submit button. See the screenshot below:

Remember that we have not used a database to store the data for registered users.

In this article, we will go over how to perform number, or numeric, validation of a form field filled in by a user, to make sure that the data entered is, in fact, a numeric value.

This may be necessary for a slew of forms filled out on the web. Examples of these are credit card numbers, zip codes, house numbers, telephone numbers, cash amounts, etc. These are all form fields where we want only numbers entered in to the form field. If any characters in the sequence are non-numeric, then we want to tell the user that the sequence of characters which they entered in are not valid. This is what is referred to as number validation.

Below is an example of a form field which checks for number validation. Only numbers can be entered in to the form field, only numeric values. If non-numeric values are entered, then the form field catches this and tells the user that s/he has made an error.

Result

Above is a form which checks for number validation. In this form, it is wanted that users enter numbers only into the form field. If a user enters a nonnumeric character, an error is thrown and the user is told to enter numbers only into the field.

PHP

So how do we perform number validation in PHP on a form field to make sure only numbers have been entered?

The answer is, we use a function in PHP called the is_numeric function.

The is_numeric function is used to check whether the character[s] which are entered. If all the characters are numeric, it returns a true value. If all the characters are not numeric, it returns a false, or not true, value.

Knowing now how this function works, we can implement it based on an if-else statement. If true, we can make it execute one statement. If false, we can make it execute another statement, such as the form above does.

HTML Code

This HTML code creates the text field in which a user enters characters.

The important thing we need to know from this HTML Code is the "name" attribute of the text field, which in this case is number. We will need to know this for the PHP code, because the "name" attribute is what we use in PHP of the HTML form field to test the characters that the user enters into this field.

PHP Code

What is form validation in PHP?

Form Validation is a necessary process before the data entered in the form is submitted to the database. This is done to avoid unnecessary errors. In PHP Form validation, the script checks for data in respective fields based on the rules set by the developer, and returns an error if it does not meet the requirements.

How do I validate a number?

Best practices for phone number validation.

Confirm that the phone number is valid. ... .

Check for line type, including mobile, landline and VoIP numbers. ... .

Build an allow list of country codes to accept. ... .

Maintain a list of carriers based on reputation..

How will you create forms and validate the form input in PHP?

PHP validates the data at the server-side, which is submitted by HTML form. You need to validate a few things: Empty String..

$name = $_POST ["Name"];.

if [! preg_match ["/^[a-zA-z]*$/", $name] ] {.

$ErrMsg = "Only alphabets and whitespace are allowed.";.

echo $ErrMsg;.

} else {.

echo $name;.

How many types of validation are there in PHP?

There are basically 2 attributes we need to specify while validating form element: action and method.

Chủ Đề