Search code examples
polymer

Error in console while validating gstin in Polymer JS


I have a Polymer JS code which validates GST Identification number:

<dom-module id="gst">
    <template>
            <div>
                <label for="gstid" class="gstlabel">GSTIN</label>
                <input type="text" id="gstid" name="gstid" on-submit="gstid_validation(gstid)">
            </div>
    </template>

    <script>
        Polymer({
            is: 'gst',
            gstid_validation: function(gstin) {
                var gstin_count = length(gstin);
                var pattern = "qr/\d{2}[A-Z]{5}\d{4}[A-Z][A-Z\d]Z[A-Z\d]/";
                if (gstin_count !== 15 && gstin !~ pattern) {
                    return 'Invalid GSTIN';
                }
            }.
        });
    </script>
</dom-module>

I get the following error in console:

Uncaught SyntaxError: Unexpected token !

Can anyone please help?

Thanks in advance


Solution

  • As I wrote in the comment also, that is clearly a problem with the second part of your if statement, gstin !~ pattern.

    Assuming that what you want is to match a regex the correct syntax would be something like:

    if (gstin_count !== 15 && pattern.text(gstin))
    

    Please note that there are several other issues with your code. Just to name a few:

    • Your pattern variable holds a string, not a regex. For a regex you wouldn't use the quotations as delimiters. Also modifiers go at the end of regexes.
    • In JS there's no length function, so if you don't have it customly implemented most likely you want gstin.length
    • custom elements need to have dashes in their name (gstin-validator, gst-in, etc),
    • when you define an event handler you need to pass the method name, not to call it, like I told you in this other post: Error in console while hiding the link on click through Polymer/Dom