newbie questions on TextBox and DateTime

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hi,

I have a TextBox in an aspx form to allow user to enter a date. Say the
TextBox name is txtDate. I have 2 questions:
(1)How do I put today's date as default value?
I tried: txtDate.Text = DateTime.Today.ToString();
I got 4/9/2004 12:00:00 AM, but I don't want the hour part.

(2)How do I do a client side validation that the value user enter is valid?

Thanks in Advance
 
Client side validation has to be done in JavaScript. Use regular
expressions. I don't have a date example, so here's a credit card one:

// Check for credit card valid, indicate failure to user
function validateCC()
{
var lb = document.all.lblInvalidCC;
lb.innerText = "";
if (!isvalidCC())
{
lb.innerText = "Invalid Credit Card Number";
return false;
}
return true;
}

// validate credit card depending on type - any value is OK if no credit
card type is selected
function isvalidCC()
{
var cc = document.all.txtCardNumber.value;
var sValidate = /^.*/;
if (cc !== "")
{
var dl = document.all.dlCreditCardType;
var index = 0;
// Determine selected credit card type by testing all options for
selected
do
{
if (dl.children(index).selected) { break;}
index++;
}
while (index < dl.children.length -1)

// Validate for selected credit card type

switch (dl.children(index).text)
{
case "American Express":
sValidate = /^(3[4,7]\d{2})(-?|\040?)\d{6}(-?|\040?)\d{5}$/;
break;
case "Visa":
sValidate = /^(4\d{3})(-?|\040?)(\d{4}(-?|\040?)){3}$/;
break;
case "MasterCard":
sValidate = /^(5[0-5]\d{2})(-?|\040?)(\d{4}(-?|\040?)){3}$/;
break;
case "Discover":
sValidate = /^(6011)(-?|\040?)(\d{4}(-?|\040?)){3}$/;
break;
default:
sValidate = /^.*/; //No card type selected, don't call it invalid
break;
}
}
return sValidate.test(cc);
}
 
Back
Top