我试图检查两个字段的密码输入fiels是否相同

时间:2016-04-26 10:17:09

标签: javascript jquery

我已经尝试过Jquery和Javascript以及wrk。请告诉我需要做什么,如果我点击提交按钮,那么必须比较和检查两个输入字段的密码。

<div class="registration-details">
    <input type="text" name="Name" maxlength="30" placeholder="Enter Your Name" required runat="server" />
    <input type="email"name="Email" required placeholder="Enter Your Email Address" runat="server"  />
    <input type="text" name="Number" required placeholder="Enter Your Contact Number" maxlength="10" />

    <input type="text" name="AddressLine1" placeholder="Address Line 1" maxlength="50" required />
    <input type="text" name="AddressLine2" placeholder="Address line 2" maxlength="50" />

    <input type="password" name="Password" required placeholder="Enter Password" />
    <input type="password" name="ConfirmPassword" required placeholder="Confirm Password" />

    <input type="submit" value="Submit" name="Submit" />
</div>
$('#form').submit(function() {
    var id1 = $(#Password).text();
    var id2 = $(#ConfirmPassword).text();
    if (id1 == id2) {
        alert('Error, cant do that');
        return false;
    } else {
        return true;
    }
});

4 个答案:

答案 0 :(得分:3)

尝试以下代码:

1.add输入字段:

<input type="password" id="Password" name="Password" required placeholder="Enter Password" />
<input type="password" id="ConfirmPassword" name="ConfirmPassword" required placeholder="Confirm Password" />

<强> 2。检查jquery

$('#form').submit(function() {
    var id1 = $("#Password").val(); // use .val()
    var id2 = $("#ConfirmPassword").val();
    if (id1 == id2) {
        alert('Error, cant do that');
        return false;
    } else {
        return true;
    }
});

答案 1 :(得分:0)

代码中的语法不正确:

$('#form').submit(function() {
    var id1 = $("#Password").val); //this is the right syntax
    var id2 = $("#ConfirmPassword").val();//this too
    if (id1 == id2) {
        alert('Error, cant do that');
        return false;
    } else {
        return true;
    }
});

正如其他用户正确提到的那样:

.text()用于包含文本的元素,它不适用于输入元素。

.val()是获取输入元素文本。

答案 2 :(得分:0)

缺少表单标签和 密码中缺少ID

试试这个

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  alert("hi");
  
$('form').submit(function() {
    var id1 = $('#Password').val();
    var id2 = $('#ConfirmPassword').val();
    if (id1 == id2) {
        alert('Error, cant do that');
        return false;
    } else {
        return true;
    }
});
  
  });
&#13;
<div class="registration-details">
  <form action="#" id="form">
    <input type="text" name="Name" maxlength="30" placeholder="Enter Your Name" required runat="server" />
    <input type="email"name="Email" required placeholder="Enter Your Email Address" runat="server"  />
    <input type="text" name="Number" required placeholder="Enter Your Contact Number" maxlength="10" />

    <input type="text" name="AddressLine1" placeholder="Address Line 1" maxlength="50" required />
    <input type="text" name="AddressLine2" placeholder="Address line 2" maxlength="50" />

    <input type="password" id="Password" name="Password" required placeholder="Enter Password" />
    <input type="password" id="ConfirmPassword"  name="ConfirmPassword" required placeholder="Confirm Password" />

    <input type="submit" value="Submit" name="Submit" />
    </form>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

$(document).ready(function(){
    $("#btnsubmit").on('click', function(){
      var password=$('#password').val();
       var confirmpassword=$('#confirmpassword').val();
      if(password == confirmpassword)
        {
          alert('Password and confirm password are Equal');
        }
      else
        {
           alert('Password and confirm password are not Equal');
        }
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<div class="registration-details">
    <input type="text" name="Name" maxlength="30" placeholder="Enter Your Name" required runat="server" /><br/>
    <input type="email"name="Email" required placeholder="Enter Your Email Address" runat="server"  /><br/>
    <input type="text" name="Number" required placeholder="Enter Your Contact Number" maxlength="10" /><br/>

    <input type="text" name="AddressLine1" placeholder="Address Line 1" maxlength="50" required /><br/>
    <input type="text" name="AddressLine2" placeholder="Address line 2" maxlength="50" /><br/>

    <input type="password" id="password" name="Password" required placeholder="Enter Password" /><br/>
    <input type="password" id="confirmpassword"  name="ConfirmPassword" required placeholder="Confirm Password" /><br/>

    <input type="submit" value="Submit" id="btnsubmit" name="Submit" />
</div>