user1181942
user1181942

Reputation: 1607

Is there any way user can disable server side validation in c#

I have web application in which user can enter date of birth only from one form. I have asp.net validators as well as server side validation methods that are called before save method is called.

Following is the code for server side validation:

 private bool ValidateData(out string error)
    {
        error = string.Empty;
        //Validate Date of Birth
        TextBox txtDOB = (TextBox)clientinfo.FindControl("txtDOB");
        if (!string.IsNullOrEmpty(txtDOB.Text.Trim()))
        {
            DateTime dob = Convert.ToDateTime(txtDOB.Text.Trim());

            if (dob >= DateTime.Now.Date)
            {
                error = error + "Client can not be less than 7 years and greater than 100 years old.";
            }
            else if (dob < DateTime.Now.Date && (DateTime.Now.Date.Year - dob.Year) > 100)
            {
                error = error + "Client can not be less than 7 years and greater than 100 years old.";
            }

        }
        else
        {
            error = error + "Please enter date of birth.";
        }

        return error.Length > 0 ? false : true;
    }

Some how there is one record with date of birth as '07/19/2045' (MM/DD/YYYY). Every time I test, I can see validation is working.

I can disable clint side validation from browser. But is there any way we can disable server side validation?..

I need to know how a wrong entry happened in order to prevent any future issues. Please let me know if there is any way user can override server validation or I am doing something wrong in my method.

Upvotes: 0

Views: 234

Answers (2)

Greg
Greg

Reputation: 11480

The user can't modify the server, the error more than likely exist in your conversion from a String to DateTime. You could try the following approach:

var date = DateTime.Now;
if(DateTime.TryParse(txtAge.Text, out date))
     if(date <= DateTime.Now.AddYears(-7) && date >= DateTime.Now.AddYears(-100))
         Console.WriteLine("Valid Date");

You would need to add your else statements to implement those errors, you may have to play with the comparison, but that should work for a nice starting point.

Upvotes: 3

Lakshmi Naarayanan J
Lakshmi Naarayanan J

Reputation: 11

According to me it will not be possible to disable the server side validation as I could not see any config entry which could stop the entry into this code and I think only way to handle this issue will be copy the dlls onto the local system and try out the various scenarios. Regarding the wrong entry you can do the same input in another machine or a sample console app to see which part of the validation is failing for it. Hope this helps.

Upvotes: 0

Related Questions