简体   繁体   中英

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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