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

Niall GallagherDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

0
Niall GallagherDeveloperAuthor Commented:
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
0
Julian HansenCommented:
but it doesn't seem to be working client side

Are you getting errors?

Have you changed the validation code from the original posted - to use RegEx?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Niall GallagherDeveloperAuthor Commented:
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
0
Niall GallagherDeveloperAuthor Commented:
Please let me know if I can send you anything else to help you help me.

Thanks
0
Niall GallagherDeveloperAuthor Commented:
Put me on the right track by asking what type of validation I was using
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.