ASP.NET - validation - Asked By hiren dhameliya on 14-Apr-10 07:54 AM

hi ,
give me some idea of validation of text control
suppose
 name textbox in this textbox numeric vlaue is not vaild
 numrice textbox in this text box char value is not vaild
reply me as soon as with coding .
Mash B replied to hiren dhameliya on 14-Apr-10 08:00 AM
U can use Ajax MaskEdit control for the textbox, this will solve ur problem .

ex.
<ajaxToolkit:MaskedEditExtender
    TargetControlID="TextBox2" 
    Mask="9,999,999.99"
    MessageValidatorTip="true" 
    OnFocusCssClass="MaskedEditFocus" 
    OnInvalidCssClass="MaskedEditError"
    MaskType="Number" 
    InputDirection="RightToLeft" 
    AcceptNegative="Left" 
    DisplayMoney="Left"
    ErrorTooltipEnabled="True"/>
  • MaskType - Type of validation to perform:
    None - No validation
    Number - Number validation
    Date - Date validation
    Time - Time validation
    DateTime - Date and time validation
  • Mask Characters and Delimiters
    9 - Only a numeric character
    L - Only a letter
    $ - Only a letter or a space
    C - Only a custom character (case sensitive)
    A - Only a letter or a custom character
    N - Only a numeric or custom character
    ? - Any character

    / - Date separator
    : - Time separator
    . - Decimal separator
    , - Thousand separator
    \ - Escape character
    { - Initial delimiter for repetition of masks
    } - Final delimiter for repetition of masks

    Examples:
    9999999 - Seven numeric characters
    99\/99 - Four numeric characters separated in the middle by a "/"
  •  
  • AcceptAMPM - True to display an AM/PM symbol
  • AcceptNegative - True if the negative sign (-) is allowed
    None - Do not show the negative sign
    Left - Show the negative sign on the left of the mask
    Right - Show the negative sign on the right of the mask
  • AutoComplete - True to automatically fill in empty mask characters not specified by the user
    MaskType=Number - Empty mask characters will be filled with zeros
    MaskType=Time - Empty mask characters will be filled with the current time
    MaskType=Date - Empty mask characters will be filled with the current date
    MaskType=DateTime - Empty mask characters will be filled with the current date/time
  • AutoCompleteValue - Default character to use when AutoComplete is enabled
  • Century - Default century used when a date mask only has two digits for the year
  • ClearMaskOnLostFocus - True to remove the mask when the TextBox loses focus
  • ClearTextOnInvalid - True to clear the TextBox when invalid text is entered
  • ClipboardEnabled- True to allow copy/paste with the clipboard
  • ClipboardText - Prompt text to use when a clipboard paste is performed
  • DisplayMoney - Specifies how the currency symbol is displayed
    None - Do not show the currency symbol
    Left - Show the currency symbol on the left of the mask
    Right - Show the currency symbol on the right of the mask
  • ErrorTooltipCssClass - CSS class for the tooltip message
  • ErrorTooltipEnabled - True to show a tooltip message when the mouse hovers over an invalid TextBox
  • Filtered - Valid characters for mask type "C" (case-sensitive)
  • InputDirection - Text input direction
    LeftToRight - Left to Right
    RightToLeft - Right to left
  • MessageValidatorTip - Message displayed when editing in TextBox
  • PromptChararacter - Prompt character for unspecified mask characters
  • UserDateFormat - Custom date format
  • UserTimeFormat - Custom time format
  •  
  • OnFocusCssClass - CSS class used when the TextBox receives focus
  • OnFocusCssNegative - CSS class used when the TextBox gets focus with a negative value
  • OnBlurCssNegative - CSS class used when the TextBox loses focus with a negative value
  • OnInvalidCssClass - CSS class used when the text is not valid.
  •  
  • CultureName - Name of culture to use (overrides the default page culture)
  • CultureAMPMPlaceholder - Culture override
  • CultureCurrencySymbolPlaceholder - Culture override
  • CultureDateFormat - Culture override
  • CultureDatePlaceholder - Culture override
  • CultureDecimalPlaceholder - Culture override
  • CultureThousandsPlaceholder - Culture override
  • CultureTimePlaceholder - Culture override


Web Star replied to hiren dhameliya on 14-Apr-10 08:06 AM
u can validate textbox in different way
one of simple way is using regular expression
<asp:RegularExpressionValidator runat=server
            ControlToValidate="txtNo"
            ErrorMessage="Enter numbers alone."
            ValidationExpression="^[0-9]$" />

OR
using comparevalidator

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="textbox1"



ErrorMessage="Enter numeric value" Operator="DataTypeCheck" SetFocusOnError="True"



Type="Integer"></asp:CompareValidator>


this will allow only integer values in textbox1


or else use keydown event if its a windows app,

private void textBoxNumber_KeyDown(object sender,   KeyEventArgs e )      
{ if (Char.IsDigit(e.KeyChar) == true )    
        {
        } 
        else    
        {          
           e.SuppressKeyPress = true;
    
        }           
}

or else,

write this code in ur form load event in .cs file
TextBox1.Attributes.Add("onkeypress","return CheckNemericValue(event);");//here textbox1 is ur server side control

then wite this javascript funciton in html

function CheckNemericValue(e)

 var key;
key = e.which ? e.which : e.keyCode;
if(key>=48 && key<=57)
{      
return true;
}
else
 {
 alert("please enter number only");
 return false;
 }
}

hope this will solve ur problem


for name u just put regular expression for character only

Anand Malli replied to hiren dhameliya on 14-Apr-10 08:08 AM
hi hiren

you can use RequireFieldValidator which comes with dotnet framework and which is there in your toolbox under validators tab

it has some propertieslike ControlToValidate and Message to be displayed when validation fails,so you can use that its easy and

faster...

let me know more from you chap

thxs
Kirtan Patel replied to hiren dhameliya on 14-Apr-10 08:13 AM
 
using System.Text.RegularExpressions;
protected
void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            Regex rex = new Regex("^[0-9]*");
           if( rex.Matches(TextBox1.Text.Trim()).Count < 2)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('You can not use charaters in Numeric Text Box')</script>");
            }
        }
        if (TextBox2.Text != "")
        {
            Regex rex2 = new Regex("[a-zA-Z ]*");
            if (rex2.Matches(TextBox2.Text.Trim()).Count<2)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('You can not use Numbers in Characters Text Box')</script>");
 
            }
        }
    }
Goniey N (Mr. G) replied to hiren dhameliya on 14-Apr-10 09:29 AM
In ASP.NET There Is Predefined Toolbox In "Validation" Section.

There Is a One Control Which Is "Compare Validator".


In That Property You have To Set Property "ControlToValidate" As Your Textbox Name(txtBox).

For Checking Only String Set Property "Type" As "String"

Set Another Property "ErrorMessage" As your Message(Enter Only Strings.).


So When User Will Enter Anything Than The String It Will Display your Error Message...


//This Will Work 100%......