javascript Javascript比较和验证两个文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17349609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:59:07  来源:igfitidea点击:

Javascript compare and validate two text fields

javascript

提问by Ralph E. Emeka

I have a problem i have the following javascript function:

我有一个问题,我有以下 javascript 函数:

<script type="text/javascript">
<!--
function BothFieldsIdenticalCaseSensitive() {
    var two = document.bid_deal.passphrase_hidden.value;
    var three = document.bid_deal.passphrase.value;
    if(two == three) { return true; }
    alert("Warning!! passcodes must match!!!");
    return false;
}
//-->
</script>

I am using it to compare two text fields to make sure the two text fields match themselves. it is working for one set of text fileds but has refused to work if i add another set of text field and run another instance of the same function.

我用它来比较两个文本字段,以确保两个文本字段相互匹配。它适用于一组文本字段,但如果我添加另一组文本字段并运行相同功能的另一个实例,则它拒绝工作。

I have a call javascript function calling checkEnableSubmit when the action is executed but i dont know why it won't work with more than one set of text fields. I however noticed that both call javascript behaviors in the page call the same name of checkEnableSubmit. If i change the name one stops working.

我有一个调用 javascript 函数在执行操作时调用 checkEnableSubmit,但我不知道为什么它不能用于一组以上的文本字段。然而,我注意到页面中的两个调用 javascript 行为都调用了同名的 checkEnableSubmit。如果我更改名称,则停止工作。

HERE IS THE JAVASCRIPT CALL: <input name="passphrase" type="text" id="passphrase" onfocus="clearDefault(this)" onblur="MM_callJS('checkEnableSubmit')" value="Nopass" />

这是 JAVASCRIPT 调用: <input name="passphrase" type="text" id="passphrase" onfocus="clearDefault(this)" onblur="MM_callJS('checkEnableSubmit')" value="Nopass" />

I need to implement this script to add validation to my text fields, please i need help BADLY!!!!

我需要实现这个脚本来向我的文本字段添加验证,请我需要帮助!!!!

回答by bastos.sergio

Your BothFieldsIdenticalCaseSensitivefunction can only work on the fields *passphrase_hidden* and passphrase.

您的BothFieldsIdenticalCaseSensitive函数只能对字段 *passphrase_hidden* 和passphrase起作用。

If you wish to check another two fields, then you must create another function for new fields. If, for instance, you created two fields with ids *passphrase_hidden1*, and passphrase1then you could use this function to validate their input...

如果您希望检查另外两个字段,则必须为新字段创建另一个函数。例如,如果您创建了两个 ID 为 *passphrase_hidden1* 和passphrase1 的字段,那么您可以使用此函数来验证它们的输入...

function BothFieldsIdenticalCaseSensitive2() {
    var two = document.getElementById('passphrase_hidden1').value;
    var three = document.getElementById('passphrase1').value;
    if(two == three) { return true; }
    alert("Warning!! passcodes must match!!!");
    return false;
}

another option, that you could try, would be to create one single function and in that function pass the two fields that you want to check like so:

您可以尝试的另一种选择是创建一个函数,并在该函数中传递您想要检查的两个字段,如下所示:

function BothFieldsIdenticalCaseSensitive(fieldId1, fieldId2) {
    var two = document.getElementById(fieldId1).value;
    var three = document.getElementById(fieldId2).value;
    if(two == three) { return true; }
    alert("Warning!! passcodes must match!!!");
    return false;
}

Edit

编辑

You shouldn't reference your fields with this syntax document.bid_deal.passphrase_hidden, not all browsers implement this. The standard way that works across all browsers is to call document.getElementById('passphrase_hidden').

您不应该使用此语法 document.bid_deal.passphrase_hidden 引用您的字段,并非所有浏览器都实现了这一点。适用于所有浏览器的标准方法是调用 document.getElementById('passphrase_hidden')。