Tuesday, 3 November 2015

Textbox, Email, Password, DropdownList, PhoneNo validation in Asp.Net Or Mvc

Asp.Net Validation
________________________

1. For TexBox-:
<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>

if (document.getElementById('<%=txtName.ClientID%>').value == '') {
                document.getElementById('<%=txtName.ClientID%>').focus();
                return false;
            }

2. For Email-: 
 <asp:TextBox ID="txtEmail" runat="server" class="form-control" Style="margin-left: 3%;"></asp:TextBox>

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

            if (reg.test(document.getElementById('<%=txtEmail.ClientID%>').value) == false) {
                document.getElementById('<%=txtEmail.ClientID%>').style.borderColor = "red";
                return false;
            }

3. For Password ConfirmPassword-: 
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>

<asp:TextBox ID="txtConfirmPwd" runat="server" TextMode="Password" ></asp:TextBox>


 var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
            if (passw.test(document.getElementById('<%=txtPwd.ClientID%>').value) == false) {
                alert("Password should be 6 character atleast one Upper Character, one lower character, one numeric and one special character.");
                return false;
            }

            if (document.getElementById('<%=txtPwd.ClientID%>').value != document.getElementById('<%=txtConfirmPwd.ClientID%>').value) {
                document.getElementById('<%=txtConfirmPwd.ClientID%>').style.borderColor = "red";
                return false;
            } else {
                document.getElementById('<%=txtConfirmPwd.ClientID%>').style.borderColor = "#ccc";
            }

4. For DropdownList-: 
<asp:DropDownList ID="ddlCompany" runat="server"></asp:DropDownList>

if (document.getElementById('<%=ddlCompany.ClientID%>').selectedIndex == 0) {
                document.getElementById('<%=ddlCompany.ClientID%>').focus();
                return false;
            }

5. For PhoneNo-: 
 <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>

 var regPh = /(\+91-?|0)?\d{10}/;
            if (document.getElementById('<%=txtPhone.ClientID%>').value == '') {
                document.getElementById('<%=txtPhone.ClientID%>').style.borderColor = "#ccc";
            }
            else {
                if (regPh.test(document.getElementById('<%=txtPhone.ClientID%>').value) == false) {
                    document.getElementById('<%=txtPhone.ClientID%>').style.borderColor = "red";
                    return false;
                }
                else {
                    document.getElementById('<%=txtPhone.ClientID%>').style.borderColor = "#ccc";
                }
            }

MVC Validation
______________________

1.TexBox-:
 @Html.TextBoxFor(m => m.BookingNo, new { @class = "form-control", @PlaceHolder = "Booking No" })

var bookobj = document.getElementById('BookingNo');
        if (bookobj.value == '') {
            bookobj.focus();
            return false;

        }
2.Email-:

 @Html.TextBoxFor(model => model.Email, new { @class = "form-control ", @placeholder = " Email", @type = "email" })

3.Password-:
 @Html.TextBoxFor(model => model.Password, new { @placeholder = " Password", @type = "password" })

                    <span id="errorPassword" style="color: Red;"></span>

 @Html.TextBoxFor(model => model.ConfirmPassword, new { @placeholder = " Confirm Password", @type = "password" })
                    <div id="responseDiv" style="color: Red;"></div>

 var pwd = /(?=.*\d)(?=.*[a-z]).{6,}/;
        var errorPasswordobj = document.getElementById('errorPassword');
        var Passwordobj = document.getElementById('Password');
        if (Passwordobj.value == '') {
            errorPasswordobj.innerHTML = "Password is required.";
            Passwordobj.focus();
            return false;
        }
        else {
            errorPasswordobj.innerHTML = "";
        }

        if (pwd.test(Passwordobj.value) == false) {
            errorPasswordobj.innerHTML = "More than 5 Character at least one number, one character.";
            return false;
        }

        var matched, password = $("#Password").val(), confirm = $("#ConfirmPassword").val();

        matched = (password == confirm) ? true : false;
        if (matched) {
            $("#responseDiv").html("");
        }
        else {
            $("#responseDiv").html("The password and Old password does not match.");
            return false;
        }


4.DropdownList-:
 @Html.DropDownList("RoomTypeId", new SelectList(Model.AvailableRoomTypeList, "Value", "Text", Model.RoomTypeId.ToString()), new { @class = "form-control", @id = "ddlRoomType" })

var ddlRoomType = document.getElementById("ddlRoomType");
        var selecteRoomType = ddlRoomType.options[ddlRoomType.selectedIndex].value;
        if (selecteRoomType == 0) {
            ddlRoomType.focus();
            return false;
        }
        else {
         

        }


5.//----------------- Mobile Number Validation ----------------//
@Html.TextBoxFor(a => a.Phone, new { maxlength = 10, onkeypress = "javascript:return isNumber(event)", @class = "form-last-name form-control", @onchange = "javascript:return MobileValWithUserId();" })

function isNumber(evt) {
    var iKeyCode = (evt.which) ? evt.which : evt.keyCode
    if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57)) {
        return false;
    }
    else {
        return true;
    }
}

6. //------------- Alphabet Validation ----------------//

 @Html.TextBoxFor(model => model.FirstName, new { @onkeypress = "javascript:return onlyAlphabets(event,this);", @class = "form-last-name form-control", @maxlength = 56, @onchange = "javascript:return FirstNameVal();" })

function onlyAlphabets(e, t) {
    try {
        if (window.event) {
            var charCode = window.event.keyCode;
        }
        else if (e) {
            var charCode = e.which;
        }
        else { return true; }
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || (charCode == 8) || (charCode == 46) || (charCode == 32) || (charCode == 0))
            return true;

        else
            return false;
    }
    catch (err) {
        alert(err.Description);
    }
}
7.//------------ Email Validation --------------------//
function checkEmail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (email== null || email == '' || email.length == 0) {
     
        return true;
    }
    else {
        if (!filter.test(email)) {
       
            return false;
        }
        else {
       
            return true;
        }
    }
}
8.//------------- Image Validation -----------------//

function UploadImage(image) {
    var ext = image.split('.').pop().toLowerCase();
    if ($.inArray(ext, ['png', 'jpg', 'jpeg']) == -1) {      
        return false;
    }
    else {
        return true;
    }
}

No comments:

Post a Comment