Regular expression multiline validator - asp.net

In my ASP.NET Web Form I have a multiline TextBox which should be validated with RegularExpression Validator. Text box should contain one or more strings "a" (just 'a' char, nothing else).
So far I got these regular expressions for my RegularExpressionValidator object:
(?m:(^a$)+)
(?m:\A(^a$)+\Z)
(?m:^a$)
and some others. Neither works. Guess there is something fundamental I'm not getting yet.
Could you please tell me where I'm wrong?
Here's the code involved.
A Button (just for postbacks):
<asp:Button ID="Button1" runat="server" Text="Button" />
The TextBox:
<asp:TextBox ID="TextBox1" runat="server" Rows="10" TextMode="MultiLine"></asp:TextBox>
And the regex validator:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="(?m:(^a$)+)"></asp:RegularExpressionValidator>
There is nothing else on that Web Form. I've only added those controls and modified properties. I've even did all this using VS GUI.
Using CustomValidator and doing Regex.Match(TextBox1, #"(?m:(^a$)+)") in it works just fine. Something is wrong with RegularExpressionValidator I guess.

If you want to match multiple lines, don't forget to also match the line terminators; they are not implied in $.
(?m:(^a$\r?\n?)+)
might work better.
This matches
a
or
a
a
a
etc.
And, since you're asking for a tutorial, how about regular-expressions.info?

Related

ASP.NET Regular Expression Validator VS Required Field Validator OR both?

I don't really know if I should be using both the Regular Expression Validator and Required Field Validator or what? I have just a normal form in ASP.Net (Visual Studio). I want to make sure the user types in the correct characters, such as for a phone number (which is why I want the reg ex control), but I also want the field to be required. So should these be combined or what is the best way to do this?
Right now I just have the required part working, like this:
<asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator2"
controltovalidate="TextBoxLocation" errormessage="Required"
/>
You can use both of them, but make sure to add Display="Dynamic" on both of the controls.
<asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1"
controltovalidate="TextBoxLocation" Display="Dynamic" errormessage="Required"
/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter valid Phone number" ControlToValidate="TextBoxLocation" Display="Dynamic" ValidationExpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$" ></asp:RegularExpressionValidator>
Both, simply because one validates client side and the other is server side.
This allows you to make sure the user didn't alter your clientside JavaScript and then send bad data.

is it possible to validate a control in asp.net but not require it?

Is it possible to validate that it's a phone number but not require it? The way it is right now if I don't enter a number it will throw the error. I read somewhere that you can change the RegEx to do this and I tried by enclosing the RegEx in (?:...?)
<asp:TextBox runat="server" ID="tbCompanyFax" Width="99%" Text='<%# Eval("CompanyFax")%>'></asp:TextBox>
<ajaxTK:MaskedEditExtender runat="server" ID="maskCompanyFax" AutoComplete="false" TargetControlID="tbCompanyFax" Mask="(999)999-9999" ClearMaskOnLostFocus="false" />
<asp:RegularExpressionValidator ID="regCompanyFax" runat="server" ControlToValidate="tbCompanyFax" ErrorMessage="Invalid Fax number" ValidationExpression="(?:((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4})?" Display="None"></asp:RegularExpressionValidator>
Your problem arises from the mask being submitted.
You could allow the mask in your RegEx. That would save the trouble of having to write a custom validator.
Try this RegEx
(?:((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4})|(((\(\s{3}\) ?)|(\s{3}-))?\s{3}-\s{4})
Should make an empty mask valid
I tweaked #nunepascal's RegEx and it works exactly as I wanted.
Thanks again!
ValidationExpression="(?:(((\d{3}))|(\d{3}-))?\d{3}-\d{4})|((((_{3}))|(_{3}-‌​))?_{3}-_{4})"

How can I encrypt my ValidationExpression in VB.net?

I'm very new to asp and vb.net, I've just learned about regular expression validation but one thing is bugging me: How can I hide my expression from being viewed on my source code?
The plan is to make a very simple 'login' type of page. I know this goes against all that is holy in this kind of operations, but it's just something I'd like to try out.
Also, after the expression has been validated, I want to load another page, I was thinking of achieving this with:
<asp:TextBox ID="txtcp" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="CP Errada"
Display="Dynamic" ControlToValidate="txtcp" ValidationExpression="admin"></asp:RegularExpressionValidator>
and in vb:
If txtcp is validated then
Response.Redirect("mypage.aspx")
end if
But the syntax on this IF is obviously not right, any help would be great.
You can shrink the size of your Regex Validator by using the following.
<asp:TextBox ID="txtcp" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" />
And then in your Code behind
Function Page_Load()
With RegularExpressionValidator1
.ErrorMessage="CP Errada"
.Display="Dynamic"
.ControlToValidate="txtcp"
.ValidationExpression="admin"
End With
End Function
Function SubmitButton_Clicked()
If Page.IsValid Then
Response.Redirect("mypage.aspx")
End If
End Function
Unfortunately you can't encrypt it since the whole point of validation is to use Client Side Javascript. It is bad (in fact VERY BAD) to use a regular expression to validate a username in the way that I 'think' you're doing it in your example. The right thing to do for you, honestly, is to just use the build in ASP.NET Membership Provider. It is seriously easy to learn, it's mostly secure by default, and best of all... it's "no Fuss, no Muss".

asp.net (4) listview gives me troubles with generating id's

i'm in a asp.net listview, in the itemtemplate.
<asp:ListView runat="server" ClientIDMode="Predictable" ClientIDRowSuffix="Texttranslations_key"ID="lvwTextitems">
This is my code in the itemtemplate:
<span runat="server" onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;"><%# Eval("Translation")%></span>
<asp:panel runat="server" id="EditItemDiv" style="display:none">
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
</asp:TextBox>
Now i have two problems.
First the span: i want the clientID of the asp:panel in the function openDiv(), so i can create some show hide functionality.
However, i get this as result:
<span onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;">
my code isn't seen as code, but as plain text, and i don't know why?
Second, this line gets me a runtime error (The server tag is not well formed):
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
Can somebody help me out?
ps
at first i used this code for the generation of the id's: "myid<%# Eval("Id")%>" but that didn't workout either...
ps
i'm always getting in to trouble when using the Eval and the <%# %>, so it's probably some stupid thing (i hope)
For the first part, you definitely need to be using a binding expression:
<%# EditItemDiv.ClientID %>
The <%= %> scriptlet will have no context for each item. I assume you were "paraphrasing" the syntax you say you tried, so what didn't work before?
The "server tag is not well formed" is because you are trying to use double-quotes inside double-quotes. Change the outer to single-quotes:
Text='<%# Eval("Translation")%>'>
Basically, you can't nest similar quote types. Inline script will usually demand you use double-quotes, since single-quotes have a different meaning in c#, but you can use either double or single for markup parameter quoting. The upshot is that if you need to have inline script, use single quotes to wrap the markup parameter, which frees you to use double-quotes inside it.
If you need further single quotes in the output, e.g. to render a javascript parameter, just use '. You could also use " if you wanted to render double-quotes.
OnClientClick='openDiv('EditItem(<%# Eval("something") %>');'
As stated in my comment and by jamietre to fix the binding problem you need to change the code from:
Text="<%# Eval("Translation")%>"
to
Text='<%# Eval("Translation")%>'
As for the problem with the onclick of the span, it should work as you want if you just remove the runat="server" portion. I am not sure why, but it seems that adding this causes the controls to encode the onclick property.
If you need the runat="server" on the span then I will attempt to find another solution, but there are not guarantees.

ASP.NET validate

I want to validate an asp.net texbox with min char=3, max=10 and no special characters. I do not want to use a plugin of jQuery but plain jQuery
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
How do I about it.
P.S - I asked the question earlier then edited it, but can't find the question now. Seems like it did not get posted.
In asp.net you can use the validate control.
Either a custom validator or a reg ex validator.
Something like this
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Error error..."
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Static" ></asp:RegularExpressionValidator>
Why not use a RegularExpressionValidator with the Regex pattern "[\w]{3, 10}" ?

Resources