【问题标题】:How to check any text box is not empty on jsp page?如何检查jsp页面上的任何文本框不为空?
【发布时间】:2014-03-15 08:13:07
【问题描述】:

我在 jsp 页面上有多个文本框。我想在提交时检查每个文本框是否为空。当我提交时,如果任何文本框为空,则不应允许提交到 servlet 页面。如何检查多个文本框?

<script type="text/javascript">
    function checkvalue() { 
    var mystring = document.getElementById('uname').value;
    var mystring1 = document.getElementById('pass').value; 
    if(!mystring.match(/\S/) && !mystring1.match(/\S/))
        {
            alert ('Empty value is not allowed');
            return false;
        }
    else 
        {
            alert("correct input");
            return true;
        }
    }

我已经尝试了上面的javascript。我只想知道如何减少多个文本框的代码?

【问题讨论】:

标签: jsp textbox submit


【解决方案1】:

只有 Jquery 验证和服务器端验证,但如果您仍想实现自定义验证,则应进行如下更改。

function checkvalue() { 
var returnValue = true;
$(".TxtBox").each(function(el,ue){ 
    if(!$(ue).match(/\S/)
    {
         alert("Empty value is not allowed");
         returnValue = false;
    }
    return returnValue;
})
}

【讨论】:

    【解决方案2】:
    <script type="text/javascript">
        function checkvalue() { 
        var mystring = document.getElementById('uname').value;
        var mystring1 = document.getElementById('pass').value; 
        if(!mystring.match(/\S/) && !mystring1.match(/\S/))
            {
                alert ('Empty value is not allowed');
                return false;
            }
        else 
            {
                alert("correct input");
                return true;
            }
        }
    

    我已经尝试了上面的javascript。我只是想知道如何减少多个文本框的代码?

    【讨论】:

      【解决方案3】:

      您可以使用提交按钮上的 onclick 事件来执行此操作,并删除表单标记中的操作属性(如果有)。在 onclick 事件处理函数中,您检查是否所有复选框都被选中。 document.getElementById("checkBoxId").checked - 返回真/假;在此验证之后,您可以使用 document.form['formId']​​.submit();

      在表单上提交

      如果您需要更多帮助,请告诉我。

      【讨论】:

      • 感谢您的帮助。你能告诉我,我必须在javascript中检查每个文本框吗?这将是很大的帮助...
      猜你喜欢
      • 2012-10-10
      • 2022-01-09
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多