Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts

Tuesday, October 9, 2012

CustomValidator example in asp.net

In this example, we will simply check the length of the string in the TextBox. This is a very basic and that useful example, only made to show you how you may use the CustomValidator.

Custom text:
<asp:textbox id="txtCustom" runat="server">
<asp:customvalidator controltovalidate="txtCustom" errormessage="The text must be exactly 8 characters long!" id="cusCustom" onservervalidate="cusCustom_ServerValidate" runat="server">

</asp:customvalidator></asp:textbox>


As you can see, it's pretty simple. The only unknown property is the onservervalidate event. It's used to reference a method from CodeBehind which will handle the validation. Switch to our CodeBehind file and add the following method:

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
    if(e.Value.Length == 8)
        e.IsValid = true;
    else
        e.IsValid = false;
}
This is very simple. The validator basically works by setting the e.IsValid boolean value to either true or false. Here we check the e.Value, which is the string of the control being validated, for it's length. If it's exactly 8 characters long, we return true, otherwise we return false.

Tuesday, March 20, 2012

Using javascript check uploaded file size for (Internet Explorer)

Note:- Only working with Internet explorer
for FireFox,Crome,Safari,Opera check this page

code is given below:

<html>

<head>
<script>
function getSize()
{
    var myFSO = new ActiveXObject("Scripting.FileSystemObject");
    var filepath = document.upload.file.value;
    var thefile = myFSO.getFile(filepath);
    var size = thefile.size;
    alert(size + " bytes");
}
</script>
</head>
<body>
<form name="upload">
<input type="file" name="file">
<input type="btn" value="Size?" onClick="getSize();">
</form>
</body>
</html>

Monday, March 19, 2012

Image Uploading validation using Javascript




JavaScript Function Code:- 

<script language="javascript" type="text/javascript">
            function validate() {
                var result = false;
                var upfile = document.getElementById("FileUpload1").value;
                if (upfile != "") {
                    var accept = "png,gif,jpg,jpeg,PNG,GIF,JPG,JPEG".split(',');
                    var getExtention = upfile.split('.');
                    var extention = getExtention[getExtention.length - 1];
                    for (i = 0; i < accept.length; i++) {
                        if (accept[i] == extention) {
                            result = true;
                            break;
                        }
                    }
                    if (!result) {
                        alert("allowed file extetions are png,gif,jpg,jpeg");
                    }

                }
                else {
                    alert("select image to Upload");
                }
                return result;
            }
        </script>


write this javascript function then use this function as given below code in your button or any other control as per use.

<INPUT TYPE=FILE NAME="FileUpload1"><BR>
<INPUT TYPE=SUBMIT VALUE="Submit" onClick="return validate();">    

<asp:Button ID="btnUploadImage" runat="server" Text="Upload Image" 
            OnClientClick="return validate();" onclick="btnUploadImage_Click"  />

Numeric key validation using JavaScript

we can check entered keyis numeric or not using javascript coding in our web page.

check example given below:-

Only Numeric key can be Entered :

html code for this:-

 <html>
<head>
<script type = "text/javascript">
    function isNumeric(keyCode)
    {    
        if (keyCode!=8) //if the key is the backspace key
         {
        if ((keyCode<48||keyCode>57) && (keyCode<96||keyCode>105))          return  false;    
    else
            return true;   
    } 
    }
    </script>
</head>
<body>
Only Numeric key Entered In text Box :
<input type="text" id="txtBox1" onkeydown = "return isNumeric(event.keyCode);"/>
</body>
</html>

Note:
          ASCII code for integers 0-9 is 48-57.
          ASCII code for extra numeric key pad's integers 0-9 is 96-105.