Javascript 如何停止提交输入中的空字段

Javascript 如何停止提交输入中的空字段,javascript,mysql,html,Javascript,Mysql,Html,我正在使用MySQL和PHP订阅新闻信函脚本。当用户输入电子邮件并单击按钮时,电子邮件将添加到数据库中 问题是,当单击按钮而不输入电子邮件时,数据库正在更新,其中包含一条空记录。如何停止提交空字段并强制用户输入电子邮件 这是我的HTML: <form id="myForm" action="update.php" method="post"> <input type="hidden" name="action" value="update" /> <

我正在使用MySQL和PHP订阅新闻信函脚本。当用户输入电子邮件并单击按钮时,电子邮件将添加到数据库中

问题是,当单击按钮而不输入电子邮件时,数据库正在更新,其中包含一条空记录。如何停止提交空字段并强制用户输入电子邮件

这是我的HTML:

<form id="myForm" action="update.php" method="post">
    <input type="hidden" name="action" value="update" />
    <input type="text" name="email" id="email" value="Enter your email here" onfocus="if (this.value == 'Enter your email here') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter your email here';}" onwebkitspeechchange="this.value = this.value.replace('Enter your email here','')"; style=" color:#999; font-size:1em;width:200px; font-style:italic; font-family:"Times  New Roman", Times, serif;"/>
    <input class="button" type="image" src="rss.png" />
 </form>

听起来,在接受用户输入并将其插入数据库之前,您需要进行一些表单验证。你这样做很危险

为什么不使用众多插件中的一个:


这是一个关于使用jquery验证插件的有用教程:

忽略示例中的样式,关注核心方面。在您的情况下,最有用的一行是:

<input id="cemail" name="email" size="25"  class="required email" />

大致上,您需要执行以下操作:

var form = $('#mtForm');

$('input').change(function(){
   if($((this).val() == ''){
       form.unbind('submit').submit(function(){
           return false;
       });
   }
   else{
       form.unbind('submit');
   }
})
  • 您应该将电子邮件字段的
    属性更改为
    占位符
    属性。可以从电子邮件输入标记中删除
    onfocus
    onwebkitspeechchange
    onblur
    代码
  • 如果这是您要进行的唯一验证类型,那么您可以使用类似这样的方法来检查空白字段(下面是用jQuery编写的)


  • 理想情况下,您可以在客户端(javascript/JQuery)和服务器端(php)验证表单

    为清晰起见,我将删除输入框上的内联代码以获得以下内容:

    <input type="text" name="email" id="email" value="Enter your email here" />
    
    获取输入框中的提示

    使用HTML5的客户端验证

    使用电子邮件格式验证创建必填字段:

    <input type="email" name="email" id="email" value="Enter your email here" required="required"/>
    
    }

    服务器端验证-update.php

    HTML5本身将阻止表单的提交,但是只有更新的浏览器支持HTML5


    希望这是有帮助的

    嗯,检查PHP脚本中是否有空字段?表单数据仍然需要在服务器端进行验证,以捕获禁用JavaScript的人或潜在的恶意攻击者。@MichaelHampton说得不错,不过从用户体验的角度来看,JavaScript验证要好得多。两者都用可能是合理的。是的,两者都用。JS添加alert()stlyee“不要做傻瓜”和PHP来保护后端。此外,请检查电子邮件地址的格式是否有效,以停止SQL注入攻击。/agree。您需要同时验证客户端和服务器端:)
    placeholder='Enter your email here'
    
    <input type="email" name="email" id="email" value="Enter your email here" required="required"/>
    
    $('#email').bind('blur', function() {
        if (!validateEmail($(this).val()) {
            // Add errors to form or other logic such as disable submit
        }
    });
    function validateEmail($email) {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        return emailReg.test($email);
    }
    
    // Require the email
    if (empty($_POST['email'])) {
        $error_message = 'You must enter an email!';
    } else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $error_message = 'Invalid email format. Example: example@example.example';
    } else { // If no errors, continue processing the form
        // process the form, enter email
    }