Why does the script not only accept the letter F?

Asked

Viewed 336 times

2

I made a regular expression to accept values from 0.0 to 10.0 and the letter F and precisely with it I’m having problems, ends up denying in Addmethod’s own msg().

    <html>
    <head>  
    <script src="jquery-2.0.3.js" type="text/javascript"></script>
    <script src="jquery.validate.js" type="text/javascript"></script>
    <script>
    $(document).ready( function() {
        //Método para verificar as horas com uso de expressão regular
        $.validator.addMethod("nota", function(value, element) {  
        return this.optional(element) || /^(([F]{0-1})|((([1]{1}[0]{1})\.([0]{1}))|((([0]{1})(\d){1}))\.(\d{1})))?$/i.test(value);  
        }, "Por favor entre com uma nota válida.");

        $("#formularioContato").validate({
        // Define as regras
            rules:{
                nota:{
                    nota: "required nota",
                    required: true
                }
            },
            messages:{
                nota:{
                    required: "Digite uma nota válida ou o conceito F",
                    range: "O valor do dia deve estar entre 0.0 a 10.0 ou F"
                }
            }
        });
    }); 
</script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
<script>
    jQuery(function($){
        $("#nota").mask("*9.9");
    });
</script>
</head>
<body><form id="formularioContato" method="post">

<div align="center">
<ul>
    <li>
    <label>Nota:</label><input type="text" id="nota" name="nota"></span><br />
    </li>
</ul>
</div>
    <input class="submit" type="submit" value="Enviar" />
</form>

</body>
</html>
  • The mask Mask("*9.9") should not be a Mask("A9.9")? http://igorescobar.github.io/jQuery-Mask-Plugin/

  • 1

    I think the expression could be ^(F|10\.0|\d\.\d)$, because "10.0" is valid but "11.0" is no longer valid... if the decimal is optional, it can be so: ^(F|10(\.0)?|\d(\.\d)?)$

  • J.Bruni that you sent me worked because it does not accept the 09.0

2 answers

1

The problem is in the rules (Rules)... substitute nota: "required nota" for nota: true

        rules:{
            nota:{
                nota: true,
                required: true
            }
        },

0

Your regular expression shouldn’t be like this:

^(([F]{0,1})|((([1]{1}[0]{1})\.([0]{1}))|((([0]{1})(\d){1}))\.(\d{1})))?$

I think you’ve made a little typo. Switched {0,1} for {0-1}.

  • It did not work, had tried a similar solution too, thank you for your attention.

  • Here it works: http://jsfiddle.net/fXac6/ Unless I’m misunderstanding the question.

  • The funny thing that he also accepts on this site http://regex101.com/ will be that it is something with the Rules ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.