In second part, I created a very simple program that check the validate
real name of users. If they insert the validate formate of real name then
we will add their name into database. If it is invalidate, we don't allow it
to pass through the database. In this program, I am using
regular expression.
PHP Code:
$regexp = "^[A-Za-z][A-Za-z' -]$";
if (ereg($regexp, $realname)) {
// Insert the realname into database
} else {
// Warn them to inser the correct realname
}
But if you don't know how to use regular expression, you probally do
it in the hard way. Here is raw php without using
regular expression
and do exactly the same like the code above.
PHP Code:
<?php
function isAlphabet($char)
{
// Check if ASCII of that character are between
// 65 and 90 which is range value of ASCII for
// character A to Z and check if it is between
// the range of 97 to 122 because it is a-z
// ASCII value.
if ((64 < ord($char)) && (ord($char) < 91)) {
return true;
} elseif ((96 < ord($char)) && (ord($char) < 123)) {
return true;
}
// If none of above conditions are true
// which mean that it is not an alphabet.
return false;
}
function checkUsername($str_username)
{
// Check each character of the string
for($i = 0; $i < strlen($str_username); $i++) {
$str_sub = substr($str_username, $i, 1);
// This first character must be alphabet.
if(!isAlphabet($str_sub) && ($i == 0))
return false;
// If character isn't one of alphabet or
// our allowed special character then it is
// not a vaildate username.
if (!isAlphabet($str_sub) && !($str_sub==" ")
&& !($str_sub=="'") && !($str_sub=="-")) {
return false;
}
}
return true;
}
$username = "Devi Jonh";
if (checkUsername($username)) {
print "validate";
} else {
print "invalidate";
}
?>
If you don't understand about the regular expression i am using on the
first program, you should read the first and second part. Here
is the link to second part:
Regular Expressions In PHP (Part 2)
From siLenTz
Which one you prefer...??