Search code examples
javascriptphpjquerywordpressrequired

JQuery: Trying to validate input field inside of Custom WordPress field


Having an issue trying to validate a custom field I created inside of The Events Calendar for WordPress where I have a number of custom fields in my Event page / post. It uses the standard post-new.php or post.php and I want to be able to validate against some of these custom fields. However, there's no ID's in these fields. Only name elements like this:

<input type="text" name="_ecp_custom_3" value="">

So I'm trying this script out:

jQuery(document).ready(function($) {
    $('#title').attr('required', true);
    $("[name='_ecp_custom_3']").attr("required", true);
});

The #title one works great but the custom one comes back with an error saying:

Parsing Error: Unexpected token _ecp_custom_3

So how can I get this field to validate using the name element instead of the ID?


Solution

  • I tried your code and it works fine for me

    So,Try to replace

    $("[name='_ecp_custom_3']").attr("required", true);
    

    with :

    $('[name="_ecp_custom_3"]').attr('required', true);
    

    so ,Sometimes a problem appears with double quotes jquery