locked
validation of code factor RRS feed

  • Question

  • User364607740 posted

    I've to develop a javascript code for validation of code factor format. The input lenght should not be greater than 3 characters long. The last character must be either 'M' or any digit between 0-9. eg. 4M or 110 or 45
    The format of code factor is like this::
    1. only 3 characters long
          I've done this by adding maxlength
    2. if all three characters are digits than number must not be greater than 150
    3. if the last digit is M the number must be less than 12 i.e 11M or 4M but not 12M or 13M

    <asp:TextBox ID="txtcode" runat="server" AutoCompleteType="Disabled"  MaxLength="3" />

    Please help!!!

    Friday, January 27, 2017 11:27 AM

All replies

  • User-707554951 posted

    Hi scala_1988,

    From your description and based on your needs, I  write some code for your reference.

      
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script>
            $(function () {
                $("#txtcode").change(function () {
                    var codevalue = $(this).val();
                    alert(codevalue);
                    var lastnumber = codevalue.substring(2);
                    var pretwonumber = codevalue.substring(0, 2);
                    alert(pretwonumber);
                    alert(lastnumber);
                    var reg = new RegExp('\\D')
                    if (lastnumber != "M" && reg.test(lastnumber)) {
                        alert(" The last character must be either 'M' or any digit between 0-9");
                    }
                    if (lastnumber == "M") {
                        var inttypevalue = parseInt(pretwonumber);
                        if (inttypevalue >= 12) {
                            alert("if the last digit is M the number must be less than 12 i.e 11M or 4M but not 12M or 13M");
                        }
                    }
                });
            })
        </script>
    
      <asp:TextBox ID="txtcode" runat="server"  AutoCompleteType="Disabled" MaxLength="3"></asp:TextBox>

    2. if all three characters are digits than number must not be greater than 150

    I am not clear about you need above. you could add code to above code based on you second requirement or provide more description about this.

    Best Regards

    Cathy

    Monday, January 30, 2017 6:14 AM