Click here to Skip to main content
14,694,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to add client side validation in MVC using jquery to check that mobile number entered in <input> is not less than 10 digits
Posted
Comments
Brian A Stephens 20-May-13 9:59am
   
It's very strange that all you are validating about a phone number is its length. Why are you not using a regex to ensure that it's all numbers, dashes, etc., of the correct format?

In View...

<input type="text" value="Mobile Number" onchange="validateMobileNumber(this);"></input>


In Script..

<pre lang="cs">function validateMobileNumber(obj) {
     var password = obj.value;
     if (password.length < 10)
         alert("mobile number entered s less than 10 digits ");
 }
   
Comments
sp1786 20-May-13 3:07am
   
but on button click i can save the value in textbox even if it is less than 10
this is what i do:
function ValidatePhone() {
debugger;
var isSuccess = false;
var phoneVal = $("#Mobile").val();
phoneVal = phoneVal.value;
var numbers = phoneVal.split("").length;
if (10 <= numbers) {
alert("SUCCESS");
isSuccess = true;
}
else {
alert("check number");
isSuccess = false;
}
return isSuccess;
}

<%: Html.TextBoxFor(model => model.Mobile, new { maxlength = 10, style = "width:145px", @onblur = "return ValidatePhone()" })%>
Try this solution..

In View ..

<html.textboxfor(model> model.Mobile, new { maxlength = 10, style = "width:145px", @onblur = "return Validate Phone()" })/>

.
.
<input type="submit" id="save" value="save" />


In Script...

<script type="text/javascript">
    function ValidatePhone() {
        debugger;
        var phoneVal = $("#Mobile").val();
        var number = phoneVal.length;
        if (10 <= number) {

            alert("SUCCESS");

            $("#save").attr('disabled', false);

        }

        else {

            alert("check number");

            $("#save").attr('disabled', true);

        }

    }

</script>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900