Reputation: 43
I have one text box on that text box i am using required field validation. If text box is empty then i want to show in the text box "Please enter some text", and if something is in text box then i want to check that text box text is valid email id or not if not then i want to show inside text box "Please enter valid email id"
Upvotes: 0
Views: 8261
Reputation: 15
For empty text validation use RequiredFieldValidator default functionality:
<asp:RequiredFieldValidator runat="server" ID="requiredValidatorId" ControlToValidate="textBoxId" ErrorMessage="Please enter some text" />
And for input format or domain validation use server side logic like this:
if (!IsValidEmail(email))
{
zoneRequiredValidatorId.Text = "Please enter valid email id";
zoneRequiredValidatorId.IsValid = false;
}
else
if (...)
{
zoneRequiredValidatorId.Text = "Validation error X";
zoneRequiredValidatorId.IsValid = false;
}
...
Please note we are setting here the IsValid property to false.
Upvotes: 0
Reputation: 2199
You can carry out the following using jQuery:
<script type="text/javascript">
function CheckEmail()
{
var $email = $("#<%=EmailText.ClientID %>");
if ($email.val().length) {
if (!IsEmailValid($email.val()))
$email.val("Please enter a valid email address");
}
else {
$email.val("Please enter some text.");
}
}
function IsEmailValid(inputvalue)
{
var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(pattern.test(inputvalue)) {
return true;
}
else {
return false;
}
}
</script>
<asp:TextBox ID="EmailText" Text="Please enter some text" runat="server" />
<asp:Button ID="SubmitButton" runat="server" OnClientClick="CheckEmail()" />
Or you could use out of the box .NET validation using the RequiredFieldValidator and RegularExpressionValidator.
Since the poster has stated client-side scripting cannot be used for validation, I've added server-side validation:
protected void SubmitButton_Click(object sender, EventArgs e)
{
if (IsFormValid())
{
//Do something...
}
}
private bool IsFormValid()
{
bool isValid = true;
if (String.IsNullOrEmpty(EmailText.Text))
{
EmailText.Text = "Please enter an email address";
isValid = false;
}
else
{
if (!Regex.IsMatch(EmailAddress.Text, @"/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/"))
{
EmailText.Text = "Email address not valid";
isValid = false;
}
}
return isValid;
}
Upvotes: 2
Reputation: 7525
please use this sample:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Please enter some text"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TextBox1"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
runat="server" ErrorMessage="Please enter valid emailid"></asp:RegularExpressionValidator>
RequiredFieldValidator validate empty textbox and then RegularExpressionValidator matches ValidationExpression, in this case it is emailid.
Upvotes: 0
Reputation: 9
you can use a reqiredfieldvalidator to check if the textbox is empty or not
<asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ControlToValidate="yourTextBox" Text="This field is required" />
and to check for email validation you can use a RegularExpressionValidator to validate the input of the email
<asp:RegularExpressionValidator runat="server" ID="emailValidator" ControlToValidate="emailTextBox" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Text="please enter a valid email address" />
Upvotes: 0
Reputation: 157
You can use a placeholder
<input type="text" placeholder="Please enter some text">
I believe you do need a jQuery plug in for that to work in Internet Explorer.
But I don't see why it should change to 'Please enter valid email id'
You could make it say 'Please enter valid email id' right at the start and add a validator script so people can't enter invalid emails.
function validateForm()
{
var x=document.forms["MyForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid email id");
return false;
}
}
Upvotes: 0