How to Validate Textbox in ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidson1
    New Member
    • Feb 2008
    • 144

    How to Validate Textbox in ASP.NET

    Hai friends,

    I am having a

    Textbox1 and Submit button

    Whenever any user press that submit button....

    i should validate such a way that , they can enter only integer between 50 and 100

    if they enter 150 or any String i should show a error...

    i dont want to use any rangevalidator , because i have to use this concept in array......

    if u know any simple coding send me....

    Thank U

    Davidson

    1 March 2008
  • SharpDeveloper
    New Member
    • Feb 2008
    • 12

    #2
    Write a Custom JavaScritp function to validate the same and attach the function to the submit button.

    In pageLoad
    btnSubmit.Attri butes.Add("onCl ick", "javascript:ret urn ValidateRangeVa lue()")

    JavaScript
    <script language="javas cript" type="text/javascript">
    function ValidateNumber( iValue)
    {
    for(var i=0;i< iValue.length;i ++)
    {
    // Check that current character is number.
    var c = iValue.charAt(i );

    if ((c < "0") || (c > "9"))
    {
    alert(c);
    return false;
    }
    }
    }
    function ValidateRangeVa lue()
    {
    var txtField=form1. document.getEle mentById('TextB ox1');
    if(txtField.val ue!="")
    {
    if (ValidateNumber (txtField.value )==false)
    {
    alert('Enter Number only');
    return false;
    }
    else
    {
    if(parseInt(txt Field.value)>=5 0 && parseInt(txtFie ld.value)<100)
    {
    return true;
    }
    else
    {
    alert('Value should be between 50-100');
    return false;
    }
    }
    }
    }
    </script>
    Hope this helps you.

    Originally posted by davidson1
    Hai friends,

    I am having a

    Textbox1 and Submit button

    Whenever any user press that submit button....

    i should validate such a way that , they can enter only integer between 50 and 100

    if they enter 150 or any String i should show a error...

    i dont want to use any rangevalidator , because i have to use this concept in array......

    if u know any simple coding send me....

    Thank U

    Davidson

    1 March 2008

    Comment

    Working...