0

i'm getting some problems about a function of the fiscal code validation, i'm newbie of Bootstrap and jquery, and with some search on internet, i tried something like this:

<script type="text/javascript">

         jQuery.validator.addMethod("codfiscale", function(id) { 
         var regex = /[A-Z]{6}[\d]{2}[A-Z][\d]{2}[A-Z][\d]{3}[A-Z]/;   
         return value. match(regex);  
         }, "Please insert a valid italian identification number");

</script>

And that's the form:

<form id="form">
                <div id="form" class="form-group col-md-4 col-md-offset-4">
                     <label for="id">Codice fiscale:</label>
                    <input type="text" name="id" class="codfiscale"/>
                </div>

                <div class="form-group col-md-4 col-md-offset-4">
                    <label for="exampleInputPassword1">Password:</label>
                    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                </div>
                <div class="form-group col-md-4 col-md-offset-4" class="col-md-3">
                    <button type="submit" OnClick="codfiscale" value="id" class="btn btn-primary center-block">Submit</button>
                </div>
            </form>

Thanks for any helps!

DarioS
  • 111
  • 1
  • 13
  • Si, Questi sono i 4 plugin che in generale leggo siano quelli necessari per la validazione: jquery.delegate.js,jquery.metadata.js,,jquery.validate.min.js, form-validation.js – DarioS Sep 06 '16 at 08:43
  • scusa mi fai vedere come richiami il validate? – Vixed Sep 06 '16 at 11:18

2 Answers2

2

Assuming that you are using the example from html.it

<script type="text/javascript">
    jQuery.validator.addMethod("codfiscale", function(value) { 
        var regex = /[A-Za-z]{6}[0-9lmnpqrstuvLMNPQRSTUV]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9lmnpqrstuvLMNPQRSTUV]{2}[A-Za-z]{1}[0-9lmnpqrstuvLMNPQRSTUV]{3}[A-Za-z]{1}/;
        return value. match(regex);  
    }, "Please insert a valid italian identification number");
    $(document).ready(function(){
        $("#form").validate();
    });
</script>

<form id="form">
    <div class="form-group col-md-4 col-md-offset-4">
        <label for="id">Codice fiscale:</label>
        <input type="text" name="id" class="form-control codfiscale required"/>
    </div>
    <div class="form-group col-md-4 col-md-offset-4">
        <label for="exampleInputPassword1">Password:</label>
        <input type="password" name="exampleInputPassword1" class="form-control required" placeholder="Password">
    </div>
    <div class="form-group col-md-4 col-md-offset-4" class="col-md-3">
        <button type="submit" class="btn btn-primary center-block">Submit</button>
    </div>
</form>
Vixed
  • 3,429
  • 5
  • 37
  • 68
2

You might try this:

<div>FISCAL ID: <input type="text" name="name" required pattern="[A-Z]{6}\d{2}[A-E,H,L,M,P,R,S,T]{1}\d{2}[A-Z]{1}\d{3}[A-Z]{1}" /></div>

<style>  
input:required:invalid {background-color: lightcoral;}
input:required:valid {background-color: lightgreen;} 
</style>