PDA

View Full Version : Exact character match validation



Jazz
05-18-2006, 05:38 PM
Hi expert, how to write the code of exact character match in a form.
For example I have a text field with 1 character length, and the user can only type in a or b, otherwise the form will pop up errror message. I tried the exact character match like ([Aa]) |([Bb]), but it doesn't work. How to get it correct. Thanks in advance!

Twey
05-18-2006, 05:59 PM
I don't understand. If you have:
<form action="">
<input type="text" name="one" maxlength="1">
</form>You can just use:
if(document.forms[0].elements['one'].value == "a" || document.forms[0].elements['one'].value == "b") {
// do whatever
} else {
window.alert("TEH ERROR");
}However, <select> elements are better suited to this task.

Jazz
05-18-2006, 07:32 PM
Thanks Twey! But I wrote this program in asp.net. I'm using ValiValidationExpression="[a]||[b]", but it doesn't work.

Twey
05-18-2006, 07:38 PM
In that case, why are you posting in the Javascript forum?

Insofar as I know, the only person here who knows ASP.NET is Otaku, who only pops on from time to time.

If that's regex, however, try (a|b).

otaku
05-18-2006, 08:39 PM
ValidationExpression="[abAB]" will do the job.

Jazz
05-18-2006, 09:30 PM
Thanks! But it doesn't work. The system didn't display any error and let me go when I enter other letters or numbers other than a or b. Would you please tell me what's wrong? Thanks!

djr33
05-18-2006, 11:09 PM
No idea myself, but to save you some time when someone who does checks out this thread,
you should post a link to your page and post your source code.
that will make it a lot easier than guessing at what may or may not be wrong.

otaku
05-18-2006, 11:38 PM
Like djr33 wrote, it's hard to say what's wrong without any code. Here is a small working sample:



<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Redirect("http://www.dynamicdrive.com")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Validation Expression Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
<asp:Button ID="MyButton" runat="server" Text="GO!" OnClick="MyButton_Click" />
<asp:RegularExpressionValidator
ID="RegExValidator"
runat="server"
ControlToValidate="MyTextBox"
ErrorMessage="Your error message here!"
ValidationExpression="[abAB]"></asp:RegularExpressionValidator>
</div>
</form>
</body>
</html>


Make sure your ControlToValidate match your TextBox ID

Twey
05-19-2006, 12:00 AM
Presuming that conforms to XML rules, you can self-close the asp:RegularExpressionValidator and asp:TextBox tags too.
What does an asp:Button render down into?

otaku
05-19-2006, 02:10 AM
it does not matter if you self close or not in the ASP.Net code, both version will produce the same HTML output:

<input ...... />

Twey
05-19-2006, 10:53 AM
I said "can," not "must" :)
And you haven't answered my question.