Niall Gallagher
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
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"
}
}
});
})
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
but it doesn't seem to be working client side
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; }
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>
but it doesn't seem to be working client side
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
That is everything I have done
ASKER
Please let me know if I can send you anything else to help you help me.
Thanks
Thanks
ASKER
Put me on the right track by asking what type of validation I was using
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.
Open in new window