【问题标题】:javascript to check if user has inputted [closed]javascript检查用户是否输入了[关闭]
【发布时间】:2011-10-24 23:28:05
【问题描述】:

我有一个表格

<form method="post" action="sendmail.php" name="Email form">

Message ID 
<input type="text" name="message_id" /><br/><br/>

Aggressive conduct 
<input type="radio" name="conduct" value="aggressive contact" /><br/><br/>

Offensive conduct 
<input type="radio" name="conduct" value="offensive conduct" /><br/><br/>

Rasical conduct 
<input type="radio" name="conduct" value="Rasical conduct" /><br/><br/>

Intimidating conduct 
<input type="radio" name="conduct" value="intimidating conduct" /><br/><br/>

<input type="submit" name="submit" value="Send Mail" />
</form>

我需要某种验证 javascript:

  1. 检查用户是否输入了消息 ID 并选中了其中一​​个单选按钮。 (所以消息 id 和广播组是必填字段)
  2. 如果未设置任何必填字段,则会显示错误消息。
  3. 在输入必填字段之前阻止表单提交。

谁能帮忙?

【问题讨论】:

  • 你想什么时候检查这个?
  • 这需要一个巨大的答案。您为什么不按照一些在线教程进行验证?还要记得检查服务器端,以防有人禁用了 JavaScript。
  • 自己尝试一下,如果遇到麻烦,请寻求有关现有代码的帮助。good starting point - 只是 Google 搜索的热门结果之一。

标签: javascript html validation


【解决方案1】:

您必须在提交前进行验证。

向表单添加一个 on submit 事件处理程序(将在提交表单时调用):

您可以通过两种方式做到这一点:

<form method="post" action="sendmail.php" name="Email form" onsubmit=validate()>

&lt;script&gt; 部分:

window.onload = init;

function init()
    {
         document.forms["Email form"].onsubmit = function() 
                                                           { 
                                                               validate(); 
                                                               return false; 
                                                           };
    }

现在编写验证函数本身(以上两个选项相同):

function validate()
{
    var form = document.forms["Email form"]; //Try avoiding space in form name.
    if(form.elements["message_id"].value == "") //No value in the "message_id" box
    {
        alert("Enter Message Id");
        //Alert is not a very good idea. 
        //You may want to add a span per element for the error message
        //An div/span at the form level to populate the error message is also ok
        //Populate this div or span with the error message
        //document.getElementById("errorDivId").innerHTML = "No message id";

        return false;  //There is an error. Don't proceed with form submission.

    }
}

【讨论】:

  • 不要使用onclick 事件进行验证。如果这个人点击进入怎么办?这将提交表单,而无需单击提交按钮。请改用&lt;form ... onsubmit="validate"&gt;
  • @Austin Hyde 有效积分。更新答案
  • @Andrew Whitaker 这就是选项 2 的原因。
  • IIRC, document.forms[] 仅适用于ids 形式,不适用于names 形式。不过我可能错了,因为我已经有一段时间没有使用该特定功能了。
  • @Austin,document.forms[] 确实适用于名称。实际上它不适用于 ids(非 IE。IE 可以互换使用它们。一个元素可以通过名称作为它的 id 访问。)
【解决方案2】:

更新。现在它应该可以工作了。

// Register onsubmit callback function (this will be called when the user submit his data).
document.forms[0].onsubmit = validate;

function validate() {
   var message = document.getElementsByTagName('input')[0].value,
       radioButtons = document.getElementsByTagName('input'),
       oneRadioChecked = false;

   //  check that message is not empty
   if (message.length  === 0) {
      alert("no message inserted");
      return false;
   }

   // check that at least one radio has been checked 
   for (var i = 0; i < radioButtons.length && !oneRadioChecked; i++) {
      oneRadioChecked = (radioButtons[i].type === "radio" &&radioButtons[i].checked);
   }

   if (!oneRadioChecked) {
      alert("no radio button checked");
      return false;
   } 
}

【讨论】:

  • @user278044 我无法让它为我工作
猜你喜欢
  • 2014-02-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 2020-04-30
相关资源
最近更新 更多