Profit
We can perform an email validation through this function.
1 | function isValidEmail($email){ |
2 | return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email); |
3 | } |
After fainted for a few seconds when i saw unreal4u finding, i decided to throw up preg_match solution instead.
1 | function isValidEmail($email){ |
2 | return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $email); |
3 | } |
PHP 5.2 and above.
1 | function fnValidateEmail($email) |
2 | { |
3 | return filter_var($email, FILTER_VALIDATE_EMAIL); |
4 | } |
Sanitize Email
We can further sanitize our email to ensure that everything is alright.
1 | function fnSanitizeEmaill($string) { |
2 | return preg_replace( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , '', $string ); |
3 | } |
PHP 5.2 and above.
1 | function fnSanitizeEmaill($url) |
2 | { |
3 | return filter_var($url, FILTER_SANITIZE_EMAIL); |
4 | } |
Validate Email Exist
This is not possible but certain validation can be use to validate email existence.
01 | function check_email($email) |
02 | { |
03 | $email_error= false; |
04 | $Email = htmlspecialchars(stripslashes(strip_tags(trim($email)))); //parse unnecessary characters to prevent exploits |
05 | if ($Email == '') { email_error = true; } |
06 | elseif (!eregi('^([a-zA-Z0-9._-])+@([a-zA-Z0-9._-])+\.([a-zA-Z0-9._-])([a-zA-Z0-9._-])+', $Email)) { email_error = true; } |
07 | else { |
08 | list($Email, $domain) =split('@', $Email, 2); |
09 | if (! checkdnsrr($domain, 'MX')) { email_error = true; } |
10 | else { |
11 | $array = array($Email, $domain); |
12 | $Email = implode('@', $array); |
13 | } |
14 | } |
15 | |
16 | if (email_error) { return false; } else{return true;} |
17 | } |
Validate Number Only
We can use PHP built-infunction to validate whether a given value is a number.
1 | function fnValidateNumber($value) |
2 | { |
3 | #is_ double($value); |
4 | #is_ float($value); |
5 | #is_ int($value); |
6 | #is_ integer($value); |
7 | return is_numeric($value); |
8 | } |
PHP 5.2 and above.
1 | function fnValidateNumber($value) |
2 | { |
3 | #return filter_var($value,FILTER_VALIDATE_FLOAT); // float |
4 | return filter_var($value, FILTER_VALIDATE_INT); # int |
5 | } |
Sanitize Number
We can force all value to be only numeric by sanitize them.
1 | function fnSanitizeNumber($str) |
2 | { |
3 | #letters and space only |
4 | return preg_match('/[^0-9]/', '', $str); |
5 | } |
PHP 5.2 and above.
1 | function fnSanitizeNumber($value)|
2 | { |
3 | #return filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT); // float |
4 | return filter_var($value, FILTER_SANITIZE_NUMBER_INT); # int |
5 | } |
Validate String Only
Sometimes to validate name we can use this function to restrict only letters and spaces.
1 | function fnValidateStringr($str) |
2 | { |
3 | #letters and space only |
4 | returnpreg_match('/^[A-Za-z\s ]+$/', $str); |
5 | } |
Sanitize String
We can sanitize it instead of validate user input.
1 | function fnSanitizeStringr($str) |
2 | { |
3 | #letters and space only |
4 | return preg_replace('/[^A-Za-z\s ]/', '', $str); |
5 | } |
PHP 5.2 and above. built-in function by PHP provides a much more powerful sanitize capability.
1 | functionfnSanitizeStringr($str) |
2 | { |
3 | return filter_var($str, FILTER_SANITIZE_STRIPPED); # only 'String' is allowed eg. '<br>HELLO</br>' => 'HELLO' |
4 | } |
Validate Alphanumeric Characters
This validates alphanumeric characters.
1 | function fnValidateAlphanumeric($string) |
2 | { |
3 | return ctype_alnum ($string); |
4 | } |
Sanitize Alphanumeric...
Regístrate para leer el documento completo.