Rolen Koh
Rolen Koh

Reputation: 729

jquery test function not running properly

I am using jquery for name validation but when I use test function it doesn't run. Here is the code

<script type="text/javascript">
    $(document).ready(function () {
        $('#msg').hide();

        $('input[name = "frequency_add_button"]').click(function () {
            var error = validate_add_form();
            if (error !== '') {
                $('#msg').show();
                $("p:first").html(error);
                return false;
            }
        });

        function validate_add_form() {
            var strname = $('#frequency_name_add').val();
            var illegal_chars = "/^.*?(?=[\^#%$\*:<>\?\{\|\}\@\!]).*$/";

            if ($.trim(strname) === '') {
                return 'Please enter frequency name';
            }
            if ($.trim(strname).length < 4 || $.trim(strname).length > 10) {
                return 'Frequency name length must be between 4 and 10 characters';
            }
            /*if(illegal_chars.test(strname))
            {
                return 'Special characters are not allowed in frequency name';
            }*/
            return '';
        }
    });
</script>

For checking blank and length this works fine but when I uncomment the commented part nothing runs at all. It is right syntax to check for illegal characters in jquery? Thanks for help.

Upvotes: 2

Views: 342

Answers (1)

Barmar
Barmar

Reputation: 780974

illegal_chars should be a RegExp, not a string. Try:

    var illegal_chars = /[\^#%$*:<>?{|}@!]/;

Upvotes: 4

Related Questions