Hello,
I'm trying to do some server-side form validation but am having a little trouble. First off, I'm trying to store all my Regular Expressions in an external file so that I can include them into any page with a form that needs validating. In the external page, I have the regexs stored like so:
<?php
$regex_email = '/\\A\\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z0-9._%-]{2,4}\\b\\z/i';
$regex_lettersnumbersunderscore = '/\\A[A-Z0-9_]+\\z/i';
$regex_lettersnumbers = '/\\A[A-Z0-9]+\\z/i';
$regex_capitalY = '/\\A[Y]\\z/';
?>
Then in the page with the form I require() this page. I then attempt to validate the form variables like this:
<?php
/***** Form Variables *****/
$fv_user_name = htmlentities(stripslashes($user_name));
$fv_user_password = htmlentities(stripslashes($user_password));
$fv_txtconfirm = htmlentities(stripslashes($txtconfirm));
$fv_user_email = htmlentities(stripslashes($user_email));
$fv_user_agreement = htmlentities(stripslashes($user_agreement));
?>
<?php
/***** Validate Sign-Up Form *****/
if (!preg_match('$regex_lettersnumbersunderscore', $fv_user_name)) {
$vald_user_name = "F";
}
if (!preg_match('$regex_lettersnumbersunderscore', $fv_user_password)) {
$vald_user_password = "F";
}
if (!preg_match('$regex_lettersnumbersunderscore', $fv_txtconfirm)) {
$vald_txtconfirm = "F";
}
if ('$fv_user_password' != '$fv_txtconfirm') {
$vald_passwordmatch = "F";
}
if (!preg_match('$regex_email', $fv_user_email)) {
$vald_user_email = "F";
}
if (!preg_match('$regex_capitalY', $fv_user_agreement)) {
$vald_user_agreement = "F";
} else {
$vald_form1 = "P";
}
?>
Unfortunately when I view this page now I get these errors:
Warning: No ending delimiter '$' found in /home/serialsa/public_html/join.php on line 84
Warning: No ending delimiter '$' found in /home/serialsa/public_html/join.php on line 87
Warning: No ending delimiter '$' found in /home/serialsa/public_html/join.php on line 90
Warning: No ending delimiter '$' found in /home/serialsa/public_html/join.php on line 96
Warning: No ending delimiter '$' found in /home/serialsa/public_html/join.php on line 99
These errors of course refer to the line numbers of the 5 if statements above.
If I enter the regular expressions directly into the if statements everything works fine, but trying to add the regular expressions to the if statements by using a variable like $regex_email causes the errors.
If I echo one of the regex variables it won't have double backslashes like it should. It'll have
/\A\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z0-9._%-]{2,4}\b\z/i
instead of
/\\A\\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z0-9._%-]{2,4}\\b\\z/i
How can I prevent the removal of the extra backslashes?
Thanks!
Peter