Link to home
Start Free TrialLog in
Avatar of Niall Gallagher
Niall GallagherFlag for Ireland

asked on

My first Custom Validation

With the help of looking at website examples and a bit of advice from other developers, I put together this for my first custom validation.
I have to admit I am totally out of my comfort zone and would like as much help as possible.
Does this look like it would work and if so how do I get it to work with my webpage. Please explain to me as if I was a complete beginner (which I am with jquery and custom clientside validation).
Do I just put it into a js file and drag it onto the header the same as all js files

$(document).ready(function () {
$.validator.addMethod("phonecheck", function (value) {
    'use strict';
    var result = true;
    if (value.Substring(1, 2) == "11") { result=false; }
    if (value.Substring(4, 2) == "11") { result=false; }
    if (value.Distinct().Count() == 1) { result=false; }
    var myArray = ["1", "0"];
    $.each(myArray, function () {
        if (value.Substring(3, 1) == this) { result=false; }
        return result;       
    });
    })

$('#myform').validate({
    rules: {
       PhoneNumber: {         
          phonecheck: true
          }
         },
          messages: {
          PhoneNumber: {
             phonecheck: "Invalid phone number"
             }
             }
             });
             })

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Without delving into the actual validation routine (which potentially has some errors) rest of it you pretty muhch have correct.
This code shows a "working" example (working in the sense that the valiation routine fires - although there are errors.
The question was how to integrate rather than debugging the validation - if you need help with the latter post more information on exactly what rules you want to implement.
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function () {

    $.validator.addMethod("phonecheck", function (value) {
        'use strict';
        var result = true;
        if (value.substring(1, 2) == "11") { result=false; }
        if (value.substring(4, 2) == "11") { result=false; }
        if (value.distinct().Count() == 1) { result=false; }
        var myArray = ["1", "0"];
        $.each(myArray, function () {
            if (value.Substring(3, 1) == this) { result=false; }
            return result;       
        });
    });

    $('#myform').validate({
        debug: true,
        rules: {
            PhoneNumber: {         
                phonecheck: true
            }
        },
        messages: {
            PhoneNumber: {
                phonecheck: "Invalid phone number"
             }
        }
    });
});
                                  
</script>
<style type="text/css">
</style>
</head>
<body>
<form id="myform">
    Phone number <input type="text" name="PhoneNumber" id="PhoneNumber" />
    <input type="submit" />
</form>
</body>
</html>

Open in new window

Avatar of Niall Gallagher

ASKER

I have a it using RegularExpression that number has to be 10 digits

Then this custom validator stops any numbers with the 2nd and 3rd digits 11,
also the 5th and 6th cannot be 11 either
Users cannot use the same number (example: 1111111111)
and also cannot start a number with 1 or 0
I think this catches it all.

I have this in my account class

 [RegularExpression(@"(^\d{10})$", ErrorMessage = "Invalid phone number")]
        [PhoneValidator(PhoneNumber = "phonecheck")]
        public string HomePhone { get; set; }

Open in new window

and this in my .cshtml page
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/phonecheck.js")" type="text/javascript"></script>

Open in new window


but it doesn't seem to be working client side
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No I'm getting no errors It just does not do anything until press the submit button (I also have code to catch this on the server side)
That is everything I have done
Please let me know if I can send you anything else to help you help me.

Thanks
Put me on the right track by asking what type of validation I was using