how to get the error message?

Posted by Tejamanimala under ASP.NET on 9/2/2013 | Points: 10 | Views : 1599 | Status : [Member] | Replies : 4
hi,i have one text box for first name,and dob for date of birth,here my doubt is,if i enter digits as 124345 in first name box,it should be show the error messages as"doesn't accept the digits,please enter first name".like that i want to get the error message for dob as"please enter the dob".how should i get it,can any one please tell me?

manimala


Responses

Posted by: Nitesh.Luharuka on: 9/2/2013 [Member] Starter | Points: 25

Up
0
Down
For ASP.Net, you can use the following for your textbox -
<asp:RegularExpressionValidator runat="server"
id="regexpSSN" ControlToValidate="txtSSN"
ValidationExpression="[a-z]*"
ErrorMessage = "You error message"
Display="Dynamic" />

Nitesh Luharuka
http://www.niteshluharuka.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/2/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
-- To allow only strings....
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 

ControlToValidate="TextBox1" ErrorMessage="Invalid Data..... Enter only Strings"
ValidationExpression="[a-zA-Z]+"></asp:RegularExpressionValidator>


Like that you should add validator for DATEformats

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/2/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
For DOB validation,
Markup:

<asp:Textbox runat="server" ID="TextBox1" />

<asp:CustomValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Date was in incorrect format" OnServerValidate="CustomValidator1_ServerValidate" />Code-behind:

protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
DateTime d;
e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
}if you want to allow several formats and only them, use next:

DateTime.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);



-- Alternate approach
<html>

<head>
<script language="javascript">
function checkDt(fld) {
var mo, day, yr;
var entry = fld.value;
var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
var valid = (reLong.test(entry)) || (reShort.test(entry));
if (valid) {
var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
var delim1 = entry.indexOf(delimChar);
var delim2 = entry.lastIndexOf(delimChar);
mo = parseInt(entry.substring(0, delim1), 10);
day = parseInt(entry.substring(delim1 + 1, delim2), 10);
yr = parseInt(entry.substring(delim2 + 1), 10);
// handle two-digit year
if (yr < 100) {
var today = new Date();
// get current century floor (e.g., 2000)
var currCent = parseInt(today.getFullYear() / 100) * 100;
// two digits up to this year + 15 expands to current century
var threshold = (today.getFullYear() + 15) - currCent;
if (yr > threshold) {
yr += currCent - 100;
} else {
yr += currCent;
}
}
var testDate = new Date(yr, mo - 1, day);
if (testDate.getDate() == day) {
if (testDate.getMonth() + 1 == mo) {
if (testDate.getFullYear() == yr) {
// fill field with database-friendly format
fld.value = mo + "/" + day + "/" + yr;
return true;
} else {
alert("Check the year entry.");
}
} else {
alert("Check the month entry.");
}
} else {
alert("Check the date entry.");
}
} else {
alert("Invalid date format. Enter as mm/dd/yyyy.");
}
return false;
}

</script>
</head>
<body>
<input type="text" id="txtDt" onblur="checkDt(this)" />
</body>
</html>


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/2/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can also use the java-script to do this:-
Link : http://www.aspdotnet-suresh.com/2012/07/regular-expression-to-validate-date-in.html
Happy Coding

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response