I'm trying to write C# code for a code-behind page in an ASP.project.
I'm to the point where if any of the fields are blank, the code returns the page with the missing fields highlighted in yellow, and if all the fields are filled, then the page saves session state and opens another page via a Response.Redirect.

I'd like to verify the txtPayRate.Text as a number, and verify and format the two date fields as well, but I'm not sure what the best way to do that is in an ASP project. In a Windows form, I'd TryParse the numbers to see if they would parse into double precision integers, or do a quick and dirty convert with a try/catch block. Working with server/client is new to me. Is there any best practice way to do number or date validation server-side in ASP?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class frmPersonnel : System.Web.UI.Page
{
    private string _firstName;
    private string _lastName;
    private double _payRate;
    private string _startDate;
    private string _endDate;
    bool _frmPersonnelIsValid = true;


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        // Redirect to Main page on Cancel button click
        Response.Redirect("frmMain.aspx");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (txtFirstName.Text.Trim() != "")
        {
            txtFirstName.BackColor = System.Drawing.Color.White;
            _firstName = txtFirstName.Text.Trim();
        }
        else
        {
            _frmPersonnelIsValid = false;
            txtFirstName.BackColor = System.Drawing.Color.Yellow;
        }
        if (txtLastName.Text.Trim() != "")
        {
            txtLastName.BackColor = System.Drawing.Color.White;
            _lastName = txtLastName.Text.Trim();
        }
        else
        {
            _frmPersonnelIsValid = false;
            txtLastName.BackColor = System.Drawing.Color.Yellow;
        }
        if (txtPayRate.Text.Trim() != "")
        {
            txtPayRate.BackColor = System.Drawing.Color.White;
        }
        else
        {
            _frmPersonnelIsValid = false;
            txtPayRate.BackColor = System.Drawing.Color.Yellow;
        }
        if (txtStartDate.Text.Trim() != "")
        {
            txtStartDate.BackColor = System.Drawing.Color.White;
            _startDate = txtStartDate.Text.Trim();
        }
        else
        {
            _frmPersonnelIsValid = false;
            txtStartDate.BackColor = System.Drawing.Color.Yellow;
        }
        if (txtEndDate.Text.Trim() != "")
        {
            txtEndDate.BackColor = System.Drawing.Color.White;
            _endDate = txtEndDate.Text.Trim();
        }
        else
        {
            _frmPersonnelIsValid = false;
            txtEndDate.BackColor = System.Drawing.Color.Yellow;
        }
        if (_frmPersonnelIsValid != true) return;
        Session["txtFirstName"] = txtFirstName.Text.Trim();
        Session["txtLastName"] = txtLastName.Text.Trim();
        Session["txtPayRate"] = txtPayRate.Text.Trim();
        Session["txtStartDate"] = txtStartDate.Text.Trim();
        Session["txtEndDate"] = txtEndDate.Text.Trim();

        Response.Redirect("frmPersonnelVerified.aspx");
    }

   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["txtFirstName"] != null)
        {
            txtFirstName.Text = Session["txtFirstName"].ToString();
        }
        if (Session["txtLastName"] != null)
        {
            txtLastName.Text = Session["txtLastName"].ToString();
        }
        if (Session["txtPayRate"] != null)
        {
            txtPayRate.Text = Session["txtPayRate"].ToString();
        }
        if (Session["txtStartDate"] != null)
        {
            txtStartDate.Text = Session["txtStartDate"].ToString();
        }
        if (Session["txtEndDate"] != null)
        {
            txtEndDate.Text = Session["txtEndDate"].ToString();
        }
    }
}

Recommended Answers

All 2 Replies

You can use validators on the first page to check some of the values entered (check that something is entered, that it is a number, falls within a certain range, matches a regular expression, etc). These validators fire when a control is clicked (such as the submit button) and if any fail the user is directed back to that page with the error messages displayed. Check them out in the VS IDE, they are simple to use. drag them onto the page, link them to the control they should validate, set the validation check and the error message. They should cover most of what you need.

Use Regular Expression validation (Rejax) using method will help you for server side validation

(in method pass the value to method in method verify the regular expr is use req acceted then return true else teturn false)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.