Checkusername validating email and special strings php

This preview shows page 11 - 13 out of 19 pages.

Let’s pass a user inputted field into our checkInput function. $checkUsername = checkInput($_POST['username']); Validating Email and Special Strings PHP has a built in regular expression function called preg_match() that can be used to check for validate input strings. Validate Email Address It is common to create an account on a website and be required to enter an email address. But there is no way a company can know if that email address actually exists but it can check that the email string entered does follow a normal email address format which it must contain a @ symbol and have a domain name such as gmail.com. By using preg_match() your code can search for an @ symbol and a . between the @ and the end of the email address. Read more on preg_match $emailaddress = htmlspecialchars($_POST['emailaddress']); if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { echo "E-mail address not valid"; } The above code would make the e‐mail address required. Validate URL address Just like we can use preg_match() to check an email address format, we can use preg_match() to check the format of a URL entered into a PHP form field. $myURL = htmlspecialchars($_POST['myWebsite']); if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$myURL)) { echo “URL address not valid"; } Validate Other There will be many specifically formatted fields in PHP that will need to be validate for security reasons such as social security number, phone number, street address. Here we can take a quick look at some such cases. Digits 0‐9 only Checking if $age is a number if (preg_match("/\D/",$age)) { echo "Please enter numbers only for Age"; }
Letters a‐z and A‐Z only Here we are checking if $string is made of letters a‐z and A‐Z only. No spaces, digits or any other characters is allowed. if (preg_match("/[^a-zA-Z]/",$string)) { echo "Please enter letters a-z and A-Z only!"; } Anything but whitespace This code will show an error if $text contains of any whitespace characters (space, tab, newline): if (preg_match("/\s/",$text)) { echo "Please do not enter any spaces, tabs or new lines!"; } User Data Once the user submitted data has been validated, what will the data be used for? The possibilities are endless. Web forms are normally made with a specific purpose. Common form uses include shopping carts, account creation, guestbook, survey, Canvas quizzes. One of the most widely used form are the account registration form. The account information such as first name, last name, email address, username, password and phone number is normally validated and then entered into a back end database to be called in the future to authenticate accounts. Another common form is the email form. The email form collects the user’s name and email address to then email information related to the company directly to the user inbox. HTML form code Let's use the form we started with in this tutorial and just add a few more fields to make it more interesting. In this example we will make fields "Your name", "Subject", "E‐mail" and "Comments" required, all others optional. We will mark required field labels bold so the visitor knows which fields he/she has to fill in.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture