I am wanting to have a numbers/currency only textbox control.
So basically it should accept this kind of input
$123
523,100
$12.50
85.59
and convert this to
123
523100
12.50
85.59
and should not accept this input
F12
309$2342
,102
I have looked at validator controls but I would like to this conversion/checking completely from the client side. What would be the best way of doing this?
I also would like a clientside error(message box or something) popped up if they try to enter invalid data and prevent them from leaving the bad data in the textbox. and then when they get out of focus of the textbox it automatically converts the data into a flat number from the client side.
How would I get started in doing this? Is there something in ASP.Net that can help me? Would I need to just use the onfocus event or what event would be preferable from javascript to validate this? I'm rather inexperienced in Javascript also.. so I apologize if this is a bit noobish.
And yes, I will also validate on the server-side
You could try validating values inside a textbox using isNaN() to check if the value is a number, after each keystroke.
Anyways, this issue has been answered before here: Validate decimal numbers in JavaScript - IsNumeric()
Relying on client-side validation is a security risk if you need certain inputs rejected.
You could use the MaskedEditExtender or FilteredTextBox if you're using MS Ajax with your site. Otherwise there are lots of great javascripts out there to do client validation, especially for use with jQuery.
Just don't ignore validation on the server side.
Related
I want to show a confirmation message on click of cancel button and then redirect to another page. But the confirmation message should come only if any change has been done on the page.
Please can anyone help on this. Is it possible to achieve this at client side or at server side without comparing data ?
Thanks in advance.
I'm guessing you could have a scenario where:
A form is populated with values and each form item that you are tracking for changes also has a corresponding hidden field.
If the user clicks submit = great, but if the user clicks cancel you want to loop through each form item, comparing it to it's original value stored in the hidden field. If a change is detected you want to show a pop-up window informing the user that if they cancel their changes will lost - or something like that.
You can do all this on the client. JQuery, or Knockout (which I would use) or Backbone (I don't know this one tbh) should all provide a neat way to achieve this.
If you can't have hidden fields you might want to send the new values to the server in an Ajax post, get the server to pull the original values, compare and send back an appropriate response to the client. I'm sure there are other ways too*.
edit: like pop the original values in an array via Javascript, then compare them. Anyway, I'm sure you get the idea :)
This could be accomplished using jquery you would need to compare data since you want the message to come up when the cancel button is clicked.
jquery reference
I have read through some articles on this topic but I am still cautious about this. I am all along using ASP:Textbox but I would like to know what are the things an input textbox cannot possibly perform without using a ASP:Textbox or takes much more effort to pull off?
I have a Jquery tooltip sample which uses HTML input textbox and I am not sure if I should change all my ASP:Textboxes to HTML textboxes, the things which I need to perform on this textboxes are RequiredFieldValidation as well as storing their values into the database.
Anyone can advice me on this rookie question. Thanks.
The asp.net textbox IS a html input box from jQuerys point of view. Everything special about it is done on the server (including viewstate validation). The question you need to ask, is what value does giving it a server side reference bring to your app? Generally the answer is easy server side reference, but does that apply in your case?
As far as i know there is no visible server side programming difference between a HTML input box and an ASP Textbox. ASP.NET Validators would work perfectly both ways.
As for your tooltip concern. Jquery doesn't care if your using an ASP Textbox since your ASP textbox will end up as an HTML textbox anyways.
And for the advice - i don't recommend you changing all your ASP Textbox to HTML input boxes, its just a waste of time. You should use the CSSClass / class property to display the tooltip instead. That way it would work on your regular HTML input box and ASP Textboxes + other page elements.
How can I prevent the user from entering anything but alpha characters in my textbox?
Update
Forgot to mention that its a dynmic control (created on the form when the user clicks a button).
With built-in ASP.NET features only, you could use the <asp:RegularExpressionValidator> control, with a regular expresion like [A-Za-z]*. This will validate on server side (which should be your main concern), and give a user-friendly error message on postback. There are ways to use these controls for clientside validation as well if you're using the MVC framework, but I don't know how or how well that works in WebForms.
Using jQuery, there are endless possibilities for clientside validation as well with the jquery.validation plugin.
The AjaxControlToolkit has a FilteredTextBox extender that allows you to specify a combination of both lower- and uppercase letters.
I would set a CssClass on the dynamically created textbox, then use jQuery and some custom javascript/regex to filter any input field with that CssClass on the keyup event.
The filtered TextBox extender is good, and you could probably use that as well, for me, the jQuery solution would be a little easier and flexible.
This supposedly easy task gave me some headache. I simply want to let the user enter any text that succeeds float.TryParse into a Textboxish control.
I could use a normal TextBox and check the Text in some btnOK_Click, but this is obviously lame. Also, there is a nice built-in MaskedTextBox control, but I failed to set it's mask to be equal to float.TryParse. Also, it seems to check for validity only when a focus change occurs.
Digging around on the net brought some interesting ideas, but none of them as nice as I would like.
How did you solve this problem? Did I simply miss an obvious solution, or do I have to implement this functionality myself?
I'm aware of a few similar threads on SO, but there was no feasible solution to be found.
Update: Yes, WinForms.
Edit
Well that makes it alot easier... Just add a Validating Event Handler to your textbox
and do the TryParse in the code behind. If its invalid, prompt the user as such.
Validating will fire when the user is finished typing and moves focus from the TextBox so if you need to do on the fly checking, you could handle the TextChanged or on of the KeyPress/KeyUp Event handlers instead
Original
Is this in asp.net or winforms/wpf
If its asp.net, you could use a combination of RegularExpressionValidator (to account for comma seperation, 1 decimal point, etc...) and a RangeValidator to set the min/max values for a float.
Aside from that, the only way to guarantee it would be to wrap the textbox in an updatepanel, stick a CustomServerValidator on it, and in the server validate function, do a TryParse on the TextBox.Text value, if it succeeds, IS VALID, if it fails, NOT VALID
Be careful using Validating and validating to false. You might find that, unless you enter valid data, you can't move focus off the textbox which is a really big usability pain.
I solve this by simply trying a TryParse() on LostFocus and if the TryParse fails I color the textbox background a reddish tint to make it obvious that something is wrong.
I have an Asp.Net repeater, which contains a textbox and a checkbox. I need to add client-side validation that verifies that when the checkbox is checked, the textbox can only accept a value of zero or blank.
I would like to use one or more of Asp.Net's validator controls to accomplish this, to provide a consistent display for client side errors (server-side errors are handled by another subsystem).
The Asp:CompareValidator doesn't seem to be flexible enough to perform this kind of complex comparison, so I'm left looking at the Asp:CustomValidator.
The problem I'm running into is that there doesn't seem to be any way to pass custom information into the validation function. This is an issue because the ClientIds of the checkbox and the textbox are unknown to me at runtime (as they're part of a Repeater).
So... My options seem to be:
Pass the textbox and checkbox to the CustomValidator somehow (doesn't seem to be possible).
Find the TextBox through JavaScript based on the arguments passed in by the CustomValidator. Is this even possible, what with the ClientId being ambiguous?
Forget validation entirely, and emit custom JavaScript (allowing me to pass both ClientIds to a custom function).
Any ideas on what might be a better way of implementing this?
I think the best way would be to inherit BaseValidator in a new class, and pass those IDs to your control as attributes. You should be able to resolve the IDs within your validator, without knowing the full client side ID that is generated at runtime. You should get the data validating on the server first, and on the client second.
Can you not put the CustomValidator inside the repeater? If not, you can create it dynamically when the repeater is bound and user FindControl()
protected MyDataBound(object sender, RepeaterItemEventArgs e) {
(CheckBox)cb = (CheckBox)e.Item.FindControl("myCheckboxName");
(TextBox)tb = (TextBox)e.Item.FindControl("myTextBox");
}
...or something like that. I did the code off the top of my head.