空输入字段的JavaScript验证

时间:2022-11-13 19:45:50

Greeting, I have this input field <input name="question"/> I want to call IsEmpty function when submit clicking submit button.

您好,我有这个输入字段我想在提交时调用IsEmpty函数。

I tried the code below but did not work. any advice?!

我尝试了下面的代码,但是没有成功。任何建议? !

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=unicode"/>
        <meta content="CoffeeCup HTML Editor (www.coffeecup.com)" name="generator"/>
    </head>
    <body>


        <script language="Javascript">

            function IsEmpty(){ 

                if(document.form.question.value == "")
                {
                    alert("empty");
                }
                return;
            }


        </script>
        Question: <input name="question"/> <br/>

        <input id="insert" onclick="IsEmpty();" type="submit" value="Add Question"/> 

    </body>
</html>

9 个解决方案

#1


82  

<script type="text/javascript">
    function validateForm()
    {
        var a=document.forms["Form"]["answer_a"].value;
        var b=document.forms["Form"]["answer_b"].value;
        var c=document.forms["Form"]["answer_c"].value;
        var d=document.forms["Form"]["answer_d"].value;
        if (a==null || a=="",b==null || b=="",c==null || c=="",d==null || d=="")
        {
            alert("Please Fill All Required Field");
            return false;
        }
    }
</script>

<form method="post" name="Form" onsubmit="return validateForm()" action="">
     <textarea cols="30" rows="2" name="answer_a" id="a"></textarea>
     <textarea cols="30" rows="2" name="answer_b" id="b"></textarea>
     <textarea cols="30" rows="2" name="answer_c" id="c"></textarea>
     <textarea cols="30" rows="2" name="answer_d" id="d"></textarea>
</form>

#2


31  

See the working example here


You are missing the required <form> element. Here is how your code should be like:

您缺少必需的

元素。你的代码应该是这样的:

function IsEmpty(){
  if(document.forms['frm'].question.value === "")
  {
    alert("empty");
    return false;
  }
    return true;
}

HTML:

HTML:

<form name="frm">
  Question: <input name="question"/> <br />
  <input id="insert" onclick="return IsEmpty();" type="submit" value="Add Question"/>
</form>

#3


18  

An input field can have whitespaces, you want to prevent that:

一个输入字段可以有空格,您想要防止:

function isEmpty(str){
    return !str.replace(/^\s+/g, '').length; // boolean (`true` if field is empty)
}

Tip: Remove the ! to invert the logic if you need a function named i.e: isValid()

提示:删除!如果你需要一个叫i的函数,就可以反转这个逻辑。艾凡:isValid()

#4


13  

I would like to add required attribute in case user disabled javascript:

如果用户禁用javascript,我想添加required属性:

<input type="text" id="textbox" required/>

It works on all modern browsers.

它适用于所有现代浏览器。

#5


4  

Add an id "question" to your input element and then try this:

在输入元素中添加一个id“question”,然后尝试以下操作:

   if( document.getElementById('question').value === '' ){
      alert('empty');
    }

The reason your current code doesn't work is because you don't have a FORM tag in there. Also, lookup using "name" is not recommended as its deprecated.

当前代码不能工作的原因是没有表单标签。此外,不建议使用“name”进行查找,因为不建议使用“name”。

See @Paul Dixon's answer in this post : Is the 'name' attribute considered outdated for <a> anchor tags?

查看@Paul Dixon的回答:对于< >锚标记来说,'name'属性是否被认为过时了?

#6


4  

if(document.getElementById("question").value.length == 0)
{
    alert("empty")
}

#7


1  

if(document.getElementById("question").value == "")
{
    alert("empty")
}

#8


1  

Just add an ID tag to the input element... ie:

只需向输入元素添加ID标记……即:

and check the value of the element in you javascript:

检查你的javascript元素的值:

document.getElementById("question").value

value . getelementbyid(“问题”)

Oh ya, get get firefox/firebug. It's the only way to do javascript.

哦,那就用火狐/firebug吧。这是做javascript的唯一方法。

#9


0  

<pre>
   <form name="myform" action="saveNew" method="post" enctype="multipart/form-data">
       <input type="text"   id="name"   name="name" /> 
       <input type="submit"/>
   </form>
</pre>

<script language="JavaScript" type="text/javascript">
 var frmvalidator  = new Validator("myform");
    frmvalidator.EnableFocusOnError(false); 
    frmvalidator.EnableMsgsTogether(); 
    frmvalidator.addValidation("name","req","Plese Enter Name");
</script>

before using above code you have to add the gen_validatorv31.js file

在使用上面的代码之前,您必须添加gen_validatorv31。js文件

#1


82  

<script type="text/javascript">
    function validateForm()
    {
        var a=document.forms["Form"]["answer_a"].value;
        var b=document.forms["Form"]["answer_b"].value;
        var c=document.forms["Form"]["answer_c"].value;
        var d=document.forms["Form"]["answer_d"].value;
        if (a==null || a=="",b==null || b=="",c==null || c=="",d==null || d=="")
        {
            alert("Please Fill All Required Field");
            return false;
        }
    }
</script>

<form method="post" name="Form" onsubmit="return validateForm()" action="">
     <textarea cols="30" rows="2" name="answer_a" id="a"></textarea>
     <textarea cols="30" rows="2" name="answer_b" id="b"></textarea>
     <textarea cols="30" rows="2" name="answer_c" id="c"></textarea>
     <textarea cols="30" rows="2" name="answer_d" id="d"></textarea>
</form>

#2


31  

See the working example here


You are missing the required <form> element. Here is how your code should be like:

您缺少必需的

元素。你的代码应该是这样的:

function IsEmpty(){
  if(document.forms['frm'].question.value === "")
  {
    alert("empty");
    return false;
  }
    return true;
}

HTML:

HTML:

<form name="frm">
  Question: <input name="question"/> <br />
  <input id="insert" onclick="return IsEmpty();" type="submit" value="Add Question"/>
</form>

#3


18  

An input field can have whitespaces, you want to prevent that:

一个输入字段可以有空格,您想要防止:

function isEmpty(str){
    return !str.replace(/^\s+/g, '').length; // boolean (`true` if field is empty)
}

Tip: Remove the ! to invert the logic if you need a function named i.e: isValid()

提示:删除!如果你需要一个叫i的函数,就可以反转这个逻辑。艾凡:isValid()

#4


13  

I would like to add required attribute in case user disabled javascript:

如果用户禁用javascript,我想添加required属性:

<input type="text" id="textbox" required/>

It works on all modern browsers.

它适用于所有现代浏览器。

#5


4  

Add an id "question" to your input element and then try this:

在输入元素中添加一个id“question”,然后尝试以下操作:

   if( document.getElementById('question').value === '' ){
      alert('empty');
    }

The reason your current code doesn't work is because you don't have a FORM tag in there. Also, lookup using "name" is not recommended as its deprecated.

当前代码不能工作的原因是没有表单标签。此外,不建议使用“name”进行查找,因为不建议使用“name”。

See @Paul Dixon's answer in this post : Is the 'name' attribute considered outdated for <a> anchor tags?

查看@Paul Dixon的回答:对于< >锚标记来说,'name'属性是否被认为过时了?

#6


4  

if(document.getElementById("question").value.length == 0)
{
    alert("empty")
}

#7


1  

if(document.getElementById("question").value == "")
{
    alert("empty")
}

#8


1  

Just add an ID tag to the input element... ie:

只需向输入元素添加ID标记……即:

and check the value of the element in you javascript:

检查你的javascript元素的值:

document.getElementById("question").value

value . getelementbyid(“问题”)

Oh ya, get get firefox/firebug. It's the only way to do javascript.

哦,那就用火狐/firebug吧。这是做javascript的唯一方法。

#9


0  

<pre>
   <form name="myform" action="saveNew" method="post" enctype="multipart/form-data">
       <input type="text"   id="name"   name="name" /> 
       <input type="submit"/>
   </form>
</pre>

<script language="JavaScript" type="text/javascript">
 var frmvalidator  = new Validator("myform");
    frmvalidator.EnableFocusOnError(false); 
    frmvalidator.EnableMsgsTogether(); 
    frmvalidator.addValidation("name","req","Plese Enter Name");
</script>

before using above code you have to add the gen_validatorv31.js file

在使用上面的代码之前,您必须添加gen_validatorv31。js文件