English 中文(简体)
空窗体提交表
原标题:Empty Form Submission Javascript

我试图阻止人们通过我的网站上的联系人, 将空白电子邮件寄给我。 在我的主要网站上,

<script type="text/javascript">
function checkForm(f)
{
if (f.elements[ cf_name ].value == "" && f.elements[ cf_email ].value == "" &&          f.elements[ cf_message ].value == "")
{
    alert("You have not filled in all of the required fields.");
    return false;
}
else
{
    f.submit();
    return false;
}
}
</script>

虽然这有效, 当我在我的移动网站上执行同样内容时, 人们仍能发送空白电子邮件。

表格检查所用的 HTML 代码是:

<form action="contact.php" method="post" name="sform" onSubmit="return checkForm(this);">

因此,我的问题是,如果我如何停止提交表格, 如果字段是空的, 在一个移动设备( 特别是iPhone) 上 。

最佳回答

你确定这不是逻辑错误吗?

if (f.elements[ cf_name ].value == "" && f.elements[ cf_email ].value == "" &&          f.elements[ cf_message ].value == "")

应:

if (f.elements[ cf_name ].value == "" || f.elements[ cf_email ].value == "" ||          f.elements[ cf_message ].value == "")
问题回答

虽然客户端验证是有用的, 因为它更直接, 您不应该依赖它, 仅使用 Javascript 来进行这种检查( 如您所找到的 ) 。 如果 Javascript 工作, 写代码会很好用, 如果 Javascript 没有执行, 仍然会执行您想要的验证 。

由于您正在将表单数据发送到php脚本,所以您要在那里和客户端中进行检查,只有在表单数据有效时才发送邮件。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签