公司有一个邮件系统是PHP写的,密码策略没有验证。请求大家帮忙写写验证代码,谢谢!具体情况如下:
1、修改密码页面有三个字段:oldPassword (原密码)、newPassword (新密码)、repeatPassword (重复密码)
2、要求前台和后台都需要验证。前台使用 Javascript,后台使用php。
3、密码长度不少于7位。
4、密码必须有大小写。
5、密码包含特殊符号(~!@#$%^&*)。
6、Javascript 验证不通过拒绝提交表单,php验证不通过返回提交页面。javascript 和 php 各40分。

解决方案 »

  1.   

    不小于7位.可以用 if(document.form1.name1.value.length<7){
    }
    正则不能严长短的吧.
    其他的搞不定啊.
      

  2.   


    <?phpfunction _post($name){return isset($_POST[$name]) ? $_POST[$name] : '';}// ajax 验证
    function checkFrom(){
    // ...
    }if(_post('oldPassword')){
    $rule = '/^[\w~!@#$%^&*]{7,7}$/';
    $oldPassword = $_POST['oldPassword'];
    if(!preg_match($rule, $oldPassword)){
    echo '<script>history.back();</script>';exit;
    }
    $newPassword = $_POST['newPassword'];
    if(!preg_match($rule, $newPassword)){
    echo '<script>history.back();</script>';exit;
    }
    $repeatPassword = $_POST['repeatPassword'];
    if($oldPassword != $newPassword){
    echo '<script>history.back();</script>';exit;
    }
    }?>
    <script language="javascript">
    function checkFrom(form){
    var rule = /^[\w~!@#$%^&*]{7,7}$/;
    var oldPassword = form.oldPassword.value;
    if(!rule.test(oldPassword)){
    alert("原密码格式不正确.");
    return false;
    }
    // ajax 后台请求,看原密码是否正确(略)
    // send ajax to checkFrom ... var newPassword = form.newPassword.value;
    if(!rule.test(newPassword)){
    alert("新密码格式不正确.");
    return false;
    }
    var repeatPassword = form.repeatPassword.value;
    if(newPassword != repeatPassword){
    alert("两次密码不一致.");
    return false;
    }
    }
    </script>
    <form action="a.php" method="post" onsubmit="return checkFrom(this)">
    原密码:<input type="text" name="oldPassword">
    新密码:<input type="text" name="newPassword">
    重复新密码:<input type="text" name="repeatPassword">
    <input type="submit" name="确定">
    </form>
      

  3.   


    <?phpfunction _post($name){return isset($_POST[$name]) ? $_POST[$name] : '';}// ajax 验证
    function checkFrom(){
    // ...
    }if(_post('oldPassword')){
    $rule = '/^[\w~!@#$%^&*]{7,7}$/';
    $oldPassword = $_POST['oldPassword'];
    if(!preg_match($rule, $oldPassword)){
    echo '<script>history.back();</script>';exit;
    }
    $newPassword = $_POST['newPassword'];
    if(!preg_match($rule, $newPassword)){
    echo '<script>history.back();</script>';exit;
    }
    $repeatPassword = $_POST['repeatPassword'];
    if($oldPassword != $newPassword){
    echo '<script>history.back();</script>';exit;
    }
    }?>
    <script language="javascript">
    function checkFrom(form){
    var rule = /^[\w~!@#$%^&*]{7,7}$/;
    var oldPassword = form.oldPassword.value;
    if(!rule.test(oldPassword)){
    alert("原密码格式不正确.");
    return false;
    }
    // ajax 后台请求,看原密码是否正确(略)
    // send ajax to checkFrom ... var newPassword = form.newPassword.value;
    if(!rule.test(newPassword)){
    alert("新密码格式不正确.");
    return false;
    }
    var repeatPassword = form.repeatPassword.value;
    if(newPassword != repeatPassword){
    alert("两次密码不一致.");
    return false;
    }
    }
    </script>
    <form action="a.php" method="post" onsubmit="return checkFrom(this)">
    原密码:<input type="text" name="oldPassword">
    新密码:<input type="text" name="newPassword">
    重复新密码:<input type="text" name="repeatPassword">
    <input type="submit" name="确定">
    </form>