简体   繁体   English

在两个单词Javascript if()语句之间包含空格

[英]Include space between two words Javascript if() statement

I'm trying to validate a generic contact form that's provided by my CRM unfortunately they control the server side script that puts the information into the CRM. 我正在尝试验证由我的CRM提供的通用联系表单,不幸的是他们控制将信息放入CRM的服务器端脚本。

The name of some of the form fields have spaces in them. 某些表单字段的名称中包含空格。 How can I accommodate for that? 我怎么能适应呢?

    if ( document.contact_form.First Name.value == "" )
    {
            alert ( "Please fill in the 'First Name' box." );
            valid = false;
    }

try this: 尝试这个:

if ( document.contact_form['First Name'].value == "" )

edit: this isn't related to your question directly, but you should be aware that testing a form's value for "" does not ensure that it is not empty. 编辑:这与您的问题没有直接关系,但您应该知道,测试表单的值为“”并不能确保它不为空。 User could enter a space and your code would mark that field as valid, which is probably not what you want. 用户可以输入一个空格,您的代码会将该字段标记为有效,这可能不是您想要的。 The best way is to trim the value and then check if it is empty. 最好的方法是修剪值,然后检查它是否为空。

The code you posted doesn't seem to be PHP but JS. 您发布的代码似乎不是PHP而是JS。 Assuming that is the case then you should try the following: 假设是这种情况,那么你应该尝试以下方法:

if (document.contact_form["First Name"].value.length === 0) { ... }

Looks like you are actually validating that form with Javascript. 看起来您实际上正在使用Javascript验证该表单。

if ( document.forms['contact_form']['First Name'].value == "" )
{
        alert ( "Please fill in the 'First Name' box." );
        valid = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM