Edit 02/05/2011
Ok, so i need to make my validation clientside, so I need what asked for below in c# to actually be in jquery. I will also be validating server side so I would appreciate more input there.
Original Question
Well, im on a real noob question trip today!!!
My previous 开发者_JAVA百科question was for a specific regex. Now I have it and it works..how can I roll it out over multiple text boxes? I don't want to use multiple Regular Expression Validation tools as they would clutter up my design space and I don't think it's a very elegant solution (It's for a degree project)
Is there a method I could write? Along the lines of
public validator(string)
{
doessomething.tostring
return true/false
}
and access by
if (validator(txtsomething.text.tostring()) = true)
{
Dothis
}
else
{
dothis
}
Please be patient if my question is garbage :) Thanks``
How about creating a custom control? I've edited my answer to include jQuery as well as server side validation. I don't know what regex you're using so I've just used a simple one testing for letters only.
javascript (also include the jQuery file):
<script language="javascript" type="text/javascript">
function validateText(source, args) {
var allTextFieldsValid = true;
$(".noNumbers").each(function () {
if (!/^[a-zA-Z]*$/.test(this.value)) {
allTextFieldsValid = false;
}
});
args.IsValid = allTextFieldsValid;
}
</script>
.aspx page:
// set a specific css class on the textboxes you want to check so that jQuery can
// find them easily
<asp:TextBox ID="TextBox1" runat="server" CssClass="noNumbers"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" CssClass="noNumbers"></asp:TextBox>
<asp:Button ID="SubmitButton" runat="server" Text="submit" OnClick="Submit_Click" />
<asp:CustomValidator ID="MyValidator" runat="server"
OnServerValidate="Text_Validate" Text="Oops, sorry, no numbers!"
ClientValidationFunction="validateText"></asp:CustomValidator>
code behind:
protected void Submit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// do stuff
}
else
{
// do other stuff
}
}
protected void Text_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = true;
// I've done each textbox by id, but depending on how many you might want to loop through the controls instead
if (!IsTextValid(TextBox1.Text))
{
args.IsValid = false;
}
if (!IsTextValid(TextBox2.Text))
{
args.IsValid = false;
}
}
private bool IsTextValid(string myTextValue)
{
string myRegexString = @"^[a-zA-Z]*$";
return Regex.IsMatch(myTextValue, myRegexString);
}
Hope this helps!
I'm not sure about your exact intent, but you seem to have it basically right:
Each presuming you're handling the "TextBoxChanged" event, you'd just do something like:
if(!string.IsNullOrEmpty(MyTextBox.Text))
{
if(ValidateInput(MyTextBox.Text)
{
[do stuff here]
}
else
{
[do other stuff here]
}
}
and then your ValidateInput(string stringToCheck)
method would look something like:
private bool ValidateInput(string stringToCheck)
{
string patern = [your regex patern]
//Check to make sure that I've got this method call right- you want to make sure
//that there are matches, basically
if(Regex.Matches(patern, stringToCheck).Count >= 1) return true;
return false;
}
That would then say "If there are any matches for my regex- do one thing, if there aren't, do something else"
Well, you could find all the textbox controls in your form/page and apply the validator.
Suppose that your are using a winforms app, try this:
// .Net Framework 3.5
foreach (TextBox textBox in MyForm.Controls.OfType<TextBox>())
{
if (validator(textBox.Text) == true)
{
// Do this
}
else
{
// Do that
}
}
Let me know if it helps.
PS.: You may need to do some recursive work for control containers =/
Write event handlers for all of your TextBox Controls:
OnLoad event handler:
foreach (Control ctrl in Controls) {
TextBox tbox = ctrl as TextBox;
if (tbox != null) {
tbox.TextChanged += new EventHandler(TextBox_TextChanged);
}
}
void TextBox_TextChanged(object sender, EventArgs e) {
TextBox tbox = sender as TextBox;
if (tbox != null) {
if (validator(tbox.Text)) {
// DoThis
} else {
// DoThat
}
}
}
Think for extending Regular Expression Validator.
Developing a Validator Control
精彩评论