【问题标题】:Javascript Password Change Form Validation doesn't work on IE8Javascript 密码更改表单验证在 IE8 上不起作用
【发布时间】:2010-07-29 19:48:40
【问题描述】:

得到这个函数来验证更改密码表单中的两个字段,它似乎在 IE8 上不起作用。 FF、Opera、Safari 和 Chrome 工作。

function chk_form_pw()
{
 if(document.getElementById('new_passwd').value == '')
 {
  alert("<?php _e('Please enter New Password') ?>");
  document.getElementById('new_passwd').focus();
  return false;
 }
 if(document.getElementById('new_passwd').value.length < 5 )
 {
  alert("<?php _e('Please enter New Password minimum 5 chars') ?>");
  document.getElementById('new_passwd').focus();
  return false;
 }
 if(document.getElementById('cnew_passwd').value == '')
 {
  alert("<?php _e('Please enter Confirm New Password') ?>");
  document.getElementById('cnew_passwd').focus();
  return false;
 }
 if(document.getElementById('cnew_passwd').value.length < 5 )
 {
  alert("<?php _e('Please enter Confirm New Password minimum 5 chars') ?>");
  document.getElementById('cnew_passwd').focus();
  return false;
 }
 if(document.getElementById('new_passwd').value != document.getElementById('cnew_passwd').value)
 {
  alert("<?php _e('New Password and Confirm New Password should be same') ?>");
  document.getElementById('cnew_passwd').focus();
  return false;
 }
}

这是表格:

<form name="registerform" id="registerform" action="<?php echo get_option( 'siteurl' ).'/?page=account&chagepw=1'; ?>" method="post">
<p><label><?php _e('New Password'); ?> <span class="indicates">*</span></label></p>
<p><input type="password" name="new_passwd" id="new_passwd"  class="lostpass_textfield" /></p>
<p><label><?php _e('Confirm New Password'); ?> <span class="indicates">*</span></label></p>
<p><input type="password" name="cnew_passwd" id="cnew_passwd"  class="lostpass_textfield" /></p>
<p><input type="submit" name="Update" onclick="return chk_form_pw();" value="<?php _e('Update Password') ?>" class="btn grey"/></p>
</form>

这是 chagepw=1 的事情:

<?php
if($_POST)
{
    if($_REQUEST['chagepw'])
    {
        $new_passwd = $_POST['new_passwd'];
        if($new_passwd)
        {
            $user_id = $current_user->data->ID;
            wp_set_password($new_passwd, $user_id);
            $message1 = "Password Changed successfully.You need to sign back in.";
        }

    }else
    {
        $user_id = $userInfo['ID'];
        $user_add1 = $_POST['user_add1'];
        $user_add2 = $_POST['user_add2'];
        $user_city = $_POST['user_city'];
        $user_state = $_POST['user_state'];
        $user_country = $_POST['user_country'];
        $user_postalcode = $_POST['user_postalcode'];

        $buser_add1 = $_POST['buser_add1'];
        $buser_add2 = $_POST['buser_add2'];
        $buser_city = $_POST['buser_city'];
        $buser_state = $_POST['buser_state'];
        $buser_country = $_POST['buser_country'];
        $buser_postalcode = $_POST['buser_postalcode'];
        $user_address_info = array(
                            "user_add1"     => $user_add1,
                            "user_add2"     => $user_add2,
                            "user_city"     => $user_city,
                            "user_state"    => $user_state,
                            "user_country"  => $user_country,
                            "user_postalcode"=> $user_postalcode,
                            "buser_name"    => $_POST['buser_fname'].'  '.$_POST['buser_lname'],
                            "buser_add1"    => $buser_add1,
                            "buser_add2"    => $buser_add2,
                            "buser_city"    => $buser_city,
                            "buser_state"   => $buser_state,
                            "buser_country" => $buser_country,
                            "buser_postalcode"=> $buser_postalcode,
                            );
        update_usermeta($user_id, 'user_address_info', serialize($user_address_info)); // User Address Information Here
        $userName = $_POST['user_fname'].'  '.$_POST['user_lname'];
        $updateUsersql = "update $wpdb->users set user_nicename=\"$userName\", display_name=\"$userName\"  where ID=\"$user_id\"";
        $wpdb->query($updateUsersql);
        $message = "Information Updated successfully.";
    }
}
$userInfo = $General->getLoginUserInfo();
$user_address_info = unserialize(get_user_option('user_address_info', $user_id));
$user_add1 = $user_address_info['user_add1'];
$user_add2 = $user_address_info['user_add2'];
$user_city = $user_address_info['user_city'];
$user_state = $user_address_info['user_state'];
$user_country = $user_address_info['user_country'];
$user_postalcode = $user_address_info['user_postalcode'];
$display_name = $userInfo['display_name'];
$display_name_arr = explode(' ',$display_name);
$user_fname = $display_name_arr[0];
$user_lname = $display_name_arr[2];
$buser_add1 = $user_address_info['buser_add1'];
$buser_add2 = $user_address_info['buser_add2'];
$buser_city = $user_address_info['buser_city'];
$buser_state = $user_address_info['buser_state'];
$buser_country = $user_address_info['buser_country'];
$buser_postalcode = $user_address_info['buser_postalcode'];
$bdisplay_name = $user_address_info['buser_name'];
$display_name_arr = explode(' ',$bdisplay_name);
$buser_fname = $display_name_arr[0];
$buser_lname = $display_name_arr[2];

if($_SESSION['redirect_page'] == '')
{
    $_SESSION['redirect_page'] = $_SERVER['HTTP_REFERER'];
}
if(strstr($_SESSION['redirect_page'],'page=checkout'))
{
    $login_redirect_link = get_option( 'siteurl' ).'/?page=checkout';
}
?>  

【问题讨论】:

  • 也许您应该显示 html 以及函数的调用方式。
  • 感谢爱斯基摩血统。就是这样,希望对你有更好的帮助=)
  • 您是否遇到任何错误?请进一步描述“不起作用”的情况。
  • 在 IE8 可以在两个框中输入任何密码,不会出现警告并验证设置新密码的过程。

标签: php javascript internet-explorer-8 validation


【解决方案1】:

更流畅的解决方案是 jQuery。在那里您可以加载加载动画,将红色/绿色等颜色放在错误/正确和简化的打印输出文本上,具体取决于表单发生的情况。

http://docs.jquery.com/Downloading_jQuery下载jQuery

然后你像这样将它插入到 html 代码中:

<script type="text/javascript" src="jquery.js">
</script>

那么你可以有一个这样的valid.js:

  $(document).ready(function () {
  var validatefield = $('#validatefield');
  $('#validatefield').keyup(function () {
   var t = this; 
 if (this.value != this.lastValue) {
  if (this.timer) clearTimeout(this.timer);
  validatefield.html('<img src="images/Here_you_can_put_your_gif_animation" 

  alt="Loading..." 
  height="16" width="16"/>');

  this.timer = setTimeout(function () {
    $.ajax({

      url: 'check_field.php',
      data: 'action=check_field&field=' + encodeURIComponent(t.value),
      dataType: 'json',
      type: 'post',
 success: function (j) {
    if (j.ok) { 
            validatefield.html(j.msg); 
    }
    else if ($("#validatefield").val() == "")
    {       
    validatefield.html('');
    }

    else { 
         validatefield.html(j.msg);
    }
     }
    });
  }, 200);

  this.lastValue = this.value;
}
});
});

然后在check_field.php中,有这个;

echo json_encode(check_field($_POST['field']));

function check_field($field) {
$fieldSum = stripslashes(strip_tags(trim(htmlspecialchars($field)))); 
$response = array(); 

if (strlen($fieldSum) > 15) {
 $response = array(
   'ok' => false, 
   'msg' => "The field characters were over 15!");      
} 
 else 
      {
$response = array(
  'ok' => true, 
  'msg' => "Correct");
}
return $response;        
}

id #validatefield 是 html 代码中的一个 id,您必须创建它才能打印出来。

例子:

<div id="validatefield">Here comes the text etc, from the valid.js</div>

【讨论】:

  • 感谢 Angus,希望在没有 JQuery 的情况下保留它,但绝对是更好的选择。打算使用这个通知验证脚本:position-absolute.com/articles/… 知道更好的相似之处吗?谢谢!
  • 有很多替代品可供选择;拿你想要的。不过,我更喜欢选择不基于绝对定位的替代方案。顺便说一句:SO 新手,请接受并支持您的答案。它可以帮助您获得快速响应
【解决方案2】:

终于用之前贴的JQuery脚本搞定了:

http://code.google.com/p/form-validation-engine/downloads/list

它很容易融入我以前的代码中,并且可以在所有经过测试的浏览器中完美运行。

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2012-02-08
    相关资源
    最近更新 更多