Reputation: 185
I have following code:
<fieldset>
<p>
<label>Password*</label>
<input type="password" placeholder="Password" name="pswd" id="psd"/>
<label>Confirm Password*</label>
<input type="password" name="cpswd" placeholder=" retype Password" id="cpsd" />
<label class="obinfo">* obligatory fields</label>
</p>
</fieldset>
<?php
$pwd = $_POST['pswd'];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd))
{
echo "Your password is good.";
}
else
{
echo "Your password is bad.";
}
?>
Presently code just checks if password is good(strong) or not . It is giving error Undefined index pswd. I am a newbie in this regard. I do not know much about javascript or jQuery. Is there a way we can check the password strength and see if they are matching using php? Also do let me know way to print all the messages in the form itself. Thanks in advance.
Upvotes: 0
Views: 2995
Reputation: 290
Seriously, u can use some kind of this: http://jsfiddle.net/3QhBu/, but there is a better way - do it yourself.
$(function(){
var $spanMsg = $('<span/>', {class: 'span-msg'});
$('input#psd').on('keyup.checkPswd', function(){
var $that = $(this);
var $thisSpanMsg = $that.siblings('span.span-msg').length ? $that.siblings('span.span-msg') : $spanMsg.clone().insertAfter($that);
if (!/.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$/.test($that.val())) {
$thisSpanMsg.removeClass('span-msg-green').addClass('span-msg-red').text('Wrong password!');
} else {
$thisSpanMsg.removeClass('span-msg-red').addClass('span-msg-green').text('Password is correct!');
}
});
$('input#cpsd').on('keyup.checkPswdMatch', function(){
var $that = $(this);
var $thisSpanMsg = $that.siblings('span.span-msg').length ? $that.siblings('span.span-msg') : $spanMsg.clone().insertAfter($that);
if ($that.val() != $('input#psd').val()) {
$thisSpanMsg.removeClass('span-msg-green').addClass('span-msg-red').text('Passwords don\'t match!');
} else {
$thisSpanMsg.removeClass('span-msg-red').addClass('span-msg-green').text('Confirm password is correct!');
}
});
});
Upvotes: 0
Reputation: 864
for client side validation using jquery there are plenty of jquery plugins available some links are given you can check the demo,
http://jqueryvalidation.org/ http://www.tutorialspoint.com/javascript/javascript_form_validations.htm
http://rickharrison.github.io/validate.js/ my personal suggestion is to use the validate js. alos implement server side validation also, because javascript can be disabled in the browser.
for server side validation using php see this link
http://phpformvalidation.monjur-ul-hasan.info/
Hope it helps you.
Upvotes: 0
Reputation:
Use:
<?php
if(isset($_POST['pswd'])){//You need to check if $_POST['pswd'] is set or not as when user visits the page $_POST['pswd'] is not set.
$pwd = $_POST['pswd'];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd))
echo "Your password is good.";
else
echo "Your password is bad.";
}
?>
Upvotes: 1
Reputation: 15609
First of all, make sure that the <form>
action is either ""
or that own page name, since it looks like that's what you want it to go to.
Then, you should do
if (isset($_POST['pswd']))
{
$pwd = $_POST['pswd'];
if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $pwd)){
echo "Your password is good.";
} else {
echo "Your password is bad.";
}
}
Upvotes: 0