Log in

View Full Version : php ereg checking password and retype password



hemi519
01-20-2011, 09:34 AM
Hi All,

i wrote a code for checking password and retype password same or not .Iam unable to go further and make it work.Can anyone help me in this. This is my code

I am getting this from register .html to validate.php. The below code belongs to validate.php.In Html i am just passing the value for these options.



switch ($type) {
case 'password':
validatePassword($value);
break;
case 'repassword':
validateRepassword($value);
break;
}


function validatePassword($value) {
if(ereg("[a-zA-Z0-9^,<>?/*()&^\%$#!~`]{4,9}$", $value, $regs)) // for some reason if i keep @ in the ereg expression it gives me error//
{
echo "true";
} else {
echo "false";
}
}

function validateRepassword($value) {
if(ereg("[a-zA-Z0-9^,<>?/*()&^\%$#!~`]{4,9}$", $value, $regs))
{
echo"true";
validateCheck();
}
else {
echo "false";
}


}

Now how can i check if both are same or not and how can i check the ereg expression for address field which can accept any type of character

prasanthmj
01-21-2011, 02:46 PM
how can i check if both are same or not


like this:


if($pwd != $re_pwd)
{
//error
}



for address field which can accept any type of character
if it can accept any value why validate on the characters? You can do an empty check and a max length check.

Also see this form validation page (http://www.html-form-guide.com/php-form/php-form-validation.html)

jscheuer1
01-21-2011, 06:59 PM
About the @ symbol - did you try escaping it?

\@

instead of:

@

and where you have:

\%

are you escaping the % character? If not, it should be:

\\%

BTW, you should use preg_match, not ereg. But as long as your system accepts ereg, that's a fine point for now.

Oziam
01-22-2011, 07:10 AM
Yeah I agree with John, ereg & eregi have been deprecated since PHP 5.0.3
so you should use preg_match instead for compatability sake.

Something like below(password must be min 4 to max 9 characters, change to suit);


function checkPassword($value){
return preg_match('/^[- a-z0-9,<>@()&\%$#!~`]{4,9}$/i', $value) ? $value : false;
}

$pass1 = checkPassword(trim($_POST['password1']));
$pass2 = trim($_POST['password2']);


if($pass1){

if($pass1 === $pass2){
echo 'Passwords match';
}
else{
echo 'Passwords donot match';
}
}
else{
echo 'Invalid characters in password etc...';
}
I haven't included all the special characters as I cannot see much point of checking if you are going to allow all characters? but you could easily modify this to suit yourself, just be careful with brackets []{} you will need to escape these with a backslash.

hemi519
01-24-2011, 11:57 AM
Thanks for your suggestions but still iam unable to work as i wanted. So i am posting my code

Below is all my code and i can not change anything in validate.php. I am still unable to check, if password and confirm password are same. The other thing is index.html by default the submit button will be enabled but when i click on any filed and it is wrong it becomes as disable. what i want is when the page is loaded it should be disable and if all the fields are correct then it should be enable. Hope that is possible but i am unable to make it.