Dynamic Interpretation of Regular Expressions - c#

I have this problem where I have created a User Control which mimics a keyboard to use on an application running on a touchscreen kiosk. The keyboard can be passed a regular expression and has an 'Enter' button that when clicked will validate the input, however I was wondering whether there is a way to dynamically validate the input as it is going in and disable certain keys dependent on whether it is a valid character for the next input.
To give some context, if I pass the keyboard the regex for a UK postcode, and the user has typed 2 letters, I would then like the keyboard to disable all other letters and only enable numbers etc.
Thanks in advance.

here is what I would try.
have an onKeyUp javascript function that checks the field for letters. if it contains the 2 letters then disable the letters with javascript and change the javascript function associated the enter button (let's call this function CheckUKPostCode). In CheckUKPostCode you would validate for a UK PostCode. If the user starts out entering numbers the onKeyUp function would disable the letters and change the function associated with the enter button to USPostCode or whatever it is you need and then this function would validate the US Post Code

You might try dynamically creating a RegularExpressionValidator Control and adding it to the Page's control collection on Page_Load()
var regexValidator = new RegularExpressionValidator
{
ControlToValidate = "keyboardTexBox",
ValidationExpression =
#"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$"
};

Related

How to lock a text box to a specific amount of characters

I'm making a little password generator and I want it to lock the output text box to 10 characters, so that when the user clicks the password genenerate button it does not just continue with an endless line of randomly generated passwords.
How can I do this?
There's a control on the text box in Visual Studio.
If you click the button and they go into properties there should be something called "max length". Set that to however long you want it.
You can use Substring() function to cut the extra characters in your generated password.
For an instance
string GeneratedPassword = "1234567891345678";
textBox1.Text = GeneratedPassword.Substring(0,10);
Also the textBox1.MaxLength = 10 will not give you the desired result. It will only limit input of the character manually in the textbox. It will not limit input of characters programmatically.

required fields and asp.net

im working on an .aspx page in Visual Studio.
I want to have a text box that is followed by a drop down menu.
if the user enters any input in the text box id like for it and the drop down menu to both be required before the corresponding button can be clicked.
is the best way to do this to use a RequiredFieldValidator ?
I think what you attempt to do is Conditional Validation
This question is similar to your question for Conditional Validation ASP.NET
Yes, RequiredFieldValidator would work well for your scenario. Just be sure to enable it or disable based on 'if the user enters any input in the text box'
You can create validators for both fields and onblur of the textbox enable/disable the validators using javascript.
HTML:
<asp:TextBox runat="server" ID="txt" onblur="enableVaidators();" />
Javascript:
function enableValidators()
{
var val_Test = document.getElementById('<%=val_Test.ClientID%>');
var enableValidators = true;
// Perform check on whether to enable or disable based on your scenario
ValidatorEnable(val_Test, enableValidators);
}
how about using jquery?
everything is done on the client-side:
http://docs.jquery.com/Plugins/Validation/
I would use a CustomValidator which implemented logic based on the state of the TextBox.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

URL format validation in code behind of textbox

I m using one text box, the text box is user choice..
User can paste Text or Url's..
but User want to display a video link... must follow the given validation......
The text box text must have only one video link...
The video link format
"http://www.youtube.com/watch?v=xxxxxxx-xx&feature=g-logo&context=G2093438FOAAAAAAABAA"
(or)
"http://youtu.be/xxxxxxx-xx"
[Note: xxxxxxx-xx -video filename]
3. The above format video only allowed. because user some time
how can i do for this validation? (during validation split functions.....)
is there anything common in your URL?
if so then you can check that string is present in URL or Not
<<**** Add following code in custom validators ServerValidate Event *>>
string str = TextBox1.Text.Trim();
if(str.contains(your_matching_string))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
A RegularExpressionValidator on the TextBox would do the trick, but you'll need to do some trial and error to come up with the right regex.
Here's a start: "^http://youtu.be/[a-zA-Z0-9]{7}-[a-zA-Z0-9]{2}"
For figuring out the rest, I can at least recommend RadSoftware's Regular Expression Designer.

Add text bullets to a C# form

I am creating a form in C# and need to display text on the form. I need some of the text to show up in a bulleted, unordered list. Is it possible to do this while using a label? Or a rich text box? I am not using ASP.NET and this is for a desktop app.
My message should look like this:
To continue please selected one of the actions below:
Click ButtonA to do this action.
Click ButtonB to do this action.
Thanks!
Just to add as a suggestion to the OP, I have found that using a StringBuilder to construct multi-line messages helps me keep the formatting straight:
StringBuilder messageBuilder = new StringBuilder(200);
messageBuilder.Append("To continue, please select one of the actions below:");
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append("\t\u2022 Click Button A to do this action.");
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append("\t\u2022 Click Button B to do this action.");
MessageBox.Show(messageBuilder.ToString());
You can use ASCII or UniCode characters, and reformat your string to replace a marker character with the formatting character.
Say you defined your string like so:
var myMessage = "To continue please selected one of the actions below: * Click ButtonA to do this action. * Click ButtonB to do this action."
you could do a Regex.Replace that looked for asterisks and converted to bullets:
var formattedString = Regex.Replace(myMessage, "\*", "\r\n \u2022");
Since the string was defined on one line, we also put in a newline ("\r\n").
If you'll take a look at the Character Map (Start > Programs > Accessories > System Tools > Character Map), you can locate the keystroke "code" for most characters.
For Arial Text, the "Middle Dot" keystroke combination is:
Alt+0183
To place this in your text using Visual Studio, hold down the Alt key while typing in "0183" on the Numeric Keypad (not the keys over the alpha pad).
This should give you "·" in your text.
That's as close as I have come.
Note that other special characters (degrees - Alt+0176 °, copyright - Alt+0169 ©, and many others) can be included with this technique.
Yes you can use text below:
const string text = #"To continue please selected one of the actions below:
. Click ButtonA to do this action.
. Click ButtonB to do this action.";
label1.Text = text;
You can use special characters for the bullet as well such as *.
You could use a WebBrowser control. I.E.:
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.DocumentText 1 = "<li>Click ButtonA ...</li><li>Click ButtonB ...</li>"

How to handle Html inputs in the TextBox

I have a requirement that user can input HTML tags in the ASP.NET TextBox. The value of the textbox will be saved in the database and then we need to show it
on some other page what he had entered. SO to do so I set the ValidateRequest="false" on the Page directive.
Now the problem is that when user input somthing like :
<script> window.location = 'http://www.xyz.com'; </script>
Now its values saved in the database, but when I am showing its value in some other page It redirects me to "http://www.xyz.com" which is obvious
as the javascript catches it. But I need to find a solution as I need to show exactly what he had entered.
I am thinking of Server.HtmlEncode. Can you guide me to a direction for my requirement
Always always always encode the input from the user and then and only then persist in your database. You can achieve this easily by doing
Server.HtmlEncode(userinput)
Now, when it come time to display the content to the user decode the user input and put it on the screen:
Server.HtmlDecode(userinput)
You need to encode all of the input before you output it back to the user and you could consider implementing a whitelist based approach to what kind of HTML you allow a user to submit.
I suggest a whitelist approach because it's much easier to write rules to allow p,br,em,strong,a (for example) rather than to try and identify every kind of malicious input and blacklist them.
Possibly consider using something like MarkDown (as used on StackOverflow) instead of allowing plain HTML?
You need to escape some characters during generating the HTML: '<' -> <, '>' -> >, '&' -> &. This way you get displayed exactly what the user entered, otherwise the HTML parser would possibly recognize HTML tags and execute them.
Have you tried using HTMLEncode on all of your inputs? I personally use the Telerik RadEditor that escapes the characters before submitting them... that way the system doesn't barf on exceptions.
Here's an SO question along the same lines.
You should have a look at the HTML tags you do not want to support because of vulnerabilities as the one you described, such as
script
img
iframe
applet
object
embed
form, button, input
and replace the leading "<" by "& lt;".
Also replace < /body> and < /html>
HTML editors such as CKEditor allow you to require well-formed XHTML, and define tags to be excluded from input.

Categories

Resources