简体   繁体   English

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

[英]How can I validate form client-side with this naming convention

I have a form that has some simple javascript to validate a simple questionnaire. 我有一个表单,其中包含一些简单的javascript来验证简单的问卷。 It used the name attribute on inputs in the form but now the page is integrated with the clients CMS it needs to use a particular naming convention which I can't get to work. 它在表单的输入中使用了name属性,但是现在该页面已与客户端CMS集成在一起,它需要使用一种特殊的命名约定,而我无法使用该约定。

The old form had inputs like this: <input type="text" name="firstName" /> 旧表单具有如下输入: <input type="text" name="firstName" />

but with the cms all fields require "fields[firstName]" , eg: <input name="fields[first-name]"> 但使用cms时,所有字段都需要"fields[firstName]" ,例如: <input name="fields[first-name]">

How do I get this to work. 我如何使它工作。

Exisiting JS below: 现有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;
}

您可以将jquery与以下内容一起使用:

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

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

相关问题 如何验证客户端 google+ 登录信息? - How do I validate a client-side google+ login? 如何使用只读的HTML5验证字段客户端? - How can I validate a field client-side using HTML5 that is read only? 条纹元素:如何验证卡片元素不是空白客户端? - Stripe elements: how can I validate a card element is not blank client-side? 如何在客户端验证中验证二变量规则? - How can I validate two-variable rules in client-side validation? 使用 JS 验证客户端表单 (HTML) 上的条目时出现问题 - Problem using JS to validate entry on client-side form (HTML) 在使用 jQuery 成功进行客户端验证后,如何提交 html 表单以进行服务器端验证? - How can I submit a html form for server-side validation after successful client-side validation with jQuery? 如何通过全客户端实现来保护 Firebase? - How can I secure Firebase with an all client-side implementation? 如何在客户端访问GridView的DataKey单元? - How can I reach DataKey cells of GridView on the client-side? 如何将数据输入Javascript以进行客户端处理? - How can I input data to Javascript for client-side processing? 如何将数据从客户端表单输入传输到服务器端Nodejs脚本? - How do I transfer data from a client-side form input to a server-side Nodejs script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM