You must Sign In to post a response.
  • Category: .NET

    How can we validate year, to allow only numeric format

    Hi Team,

    Am stuck can anyone guide me on this for the below code i have to do changes.

    The if condition is only checking for null or empty, this wouldn't fix the flaw. Validate the year for numeric format

    "string Year = Request.Params[""year""];

    if (Year == null || Year.Equals(""""))
    Year = System.DateTime.Now.Year.ToString();
  • #762615

    You can try this Code for Regex c# code year validation
    numeric or not

    public bool IsInteger(String strNumber)
    Regex objNotIntPattern = new Regex("[^0-9-]");
    Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
    return !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch(strNumber);

    bool a111= IsInteger("AAA");
    bool a112= IsInteger("12");

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762704
    Hai Shashi,
    It seems that the year is getting as string so you need to check for the integer of its value as below:

    int Year =Convert.Toint32(Request.Params[""year""]);
    if (Year == 0)
    Year = System.DateTime.Now.Year;

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • Sign In to post your comments