如何使用此命名约定验证表单客户端

时间:2013-11-06 22:35:02

标签: javascript forms validation

我有一个表单,其中包含一些简单的javascript来验证简单的问卷。它在表单中的输入上使用了name属性,但现在页面与客户端CMS集成,它需要使用我无法工作的特定命名约定。

旧表单有这样的输入: <input type="text" name="firstName" />

但是使用cms所有字段都需要"fields[firstName]",例如: <input name="fields[first-name]">

如何让它发挥作用。

现有JS:

function validate_form ( )
{
    valid = true;

    if ( ( document.register.question[0].checked == false ) && ( document.register.question[1].checked == false ) && ( document.register.question[2].checked == false ) )
    {
        alert ( "Please choose an answer" );
        valid = false;
    }

    else if ( document.register.form_title.value == "" )
    {
        alert ( "Please fill in the 'Title' box." );
        valid = false;
    }

    else if ( document.register.fName.value == "" )
    {
        alert ( "Please add your First Name" );
        valid = false;
    }

    else if ( document.register.lName.value == "" )
    {
        alert ( "Please add your Last Name" );
        valid = false;
    }

    else if ( document.register.email.value == "" )
    {
        alert ( "Please add a valid email address" );
        valid = false;
    }

    else if ( document.register.country.selectedIndex == 0 )
    {
        alert ( "Please select your Country" );
        valid = false;
    }

    else if ( document.register.dob1.selectedIndex == 0 )
    {
        alert ( "Please ensure your date of birth is complete" );
        valid = false;
    }
    else if ( document.register.dob2.selectedIndex == 0 )
    {
        alert ( "Please ensure your date of birth is complete" );
        valid = false;
    }
    else if ( document.register.dob3.selectedIndex == 0 )
    {
        alert ( "Please ensure your date of birth is complete" );
        valid = false;
    }

    else if ( document.register.terms.checked == false )
    {
        alert ( "Please check the Terms & Conditions box." );
        valid = false;
    }


    return valid;
}

1 个答案:

答案 0 :(得分:0)

你可以使用jquery这样的东西:

$("input[name=fields\\[first-name\\]]").val()