views:

376

answers:

3

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;
    }
+2  A: 

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.

Jan Hančič
You have an extra `.` before the `[`, which isn't going to work.
Daniel Pryden
thanks! I have fixed that.
Jan Hančič
+2  A: 

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) { ... }
illvm
A: 

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;
}
a432511