TextItem with Price Filter - java

Hai,
I am using SmartGwt Java. To format the price using keyPressFilter, Please suggest any regular expression. It will accept only 0-9 and single decimal only.
Now I am using KeyPress handler. In this if it is empty (default) set this value "0.00", using TextItem.setEmptyDisplayValue("0.00"). So First time it will accept single decimal eventhough there is another decimal present("0.00"). Give solution for this issue.
Thanks in advance.
Regards,
Sathya.

Many different solutions are possible.
Turning an empty field into a content of "0.00" after pressing any key is surprising, perhaps even disturbing to the user; I wouldn't do that. If you're going to assist him, you have to do so thoroughly, offering the equivalent of an input editor. Otherwise, let him do his work and just check the end result when he attempts to change focus out of the field.
Regular expressions are one way to check the input; but done alone, this is a lazy and ineffective solution. You have access to the whole input text at every keystroke. You could solve your problem by checking at keyUp time whether there is already a decimal in the input text, and if so not allow another. You could do something similar to prevent entering more than 2 digits after the decimal.

Related

Is it possible to remove tags (or sequences) and relate or remember them as indexes?

I'm working with HTML tags, and I need to interpret HTML documents. Here's what I need to achieve:
I have to recognize and remove HTML tags without removing the
original content.
I have to store the index of the previously existing markups.
So here's a example. Imagine that I have the following markup:
This <strong>is a</strong> message.
In this example, we have a String sequence with 35 characters, and markedup with strong tag. As we know, an HTML markup has a start and an end, and if we interpret the start and end markup as a sequence of characters, each also has a start and an end (a character index).
Again, in the previous example, the beggining index of the open/start tag is 5 (starts at index 0), and the end index is 13. The same logic goes to the close tag.
Now, once we remove the markup, we end up with the following:
This is a message.
The question:
How can I remember with this sequence the places where I could enter the markup again?
For example, once the markup has been removed, how do I know that I have to insert the opening tag in the X position/index, and the closing tag in the Y position/index... Like so:
This is a message.
5 9
index 5 = <strong>
index 9 = </strong>
I must remember that it is possible to find the following situation:
<a>T<b attribute="value">h<c>i<d>s</a> <g>i<h>s</h></g> </b>a</c> <e>t</e>e<f>s</d>t</f>.
I need to implement this in Java. I've figured out how to get the start and end index of each tag in a document. For this, I'm using regular expressions (Pattern and Matcher), but I still do not know how to insert the tags again properly (as described). I would like a working example (if possible). It does not have to be the best example (the best solution) in the world, but only that it works the right way for any kind of situation.
If anyone has not understood my question, please comment that I will do it better.
Thanks in advance.
EDIT
People in the comments are saying that I should not use regular expressions to work with HTML. I do not care to use or not regular expressions to solve this problem, I just want to solve it, no matter how (But of course, in the most appropriate way).
I mentioned that I'm using regular expressions, but I do not mind using another approach that presents the same solution. I read that a XML parser could be the solution. Is that correct? Is there an XML parser capable of doing all this what I need?
Again, Thanks in advance.
EDIT 2
I'm doing this edition now to explain the applicability of my problem (as asked). Well, before I start, I want to say that what I'm trying to do is something I've never done before, it's not something on my area, so it may not be the most appropriate way to do it. Anyway...
I'm developing a site where users are allowed to read content but can not edit it (edit or remove text). However, users can still mark/highlight excerpts (ranges) of the content present (with some stylization). This is the big summary.
Now the problem is how to do this (in Java). On the client side, for now, I was thinking of using TinyMCE to enable styling of content without text editing. I could save stylized text to a database, but this would take up a lot of space, since every client is allowed to do this, given that they are many clients. So if a client marks snippets of a paragraph, saving the paragraph back in the database for each client in the system is somewhat costly in terms of memory.
So I thought of just saving the range (indexes) of the markups made by users in a database. It is much easier to save just a few numbers than all the text with the styling required. In the case, for example, I could save a line / record in a table that says:
In X paragraph, from Y to Z index, the user P defined a ABC
stylization.
This would require a translation / conversion, from database to HTML, and HTML to database. Setting a converter can be easy (I guess), but I do not know how to get the indexes (following this logic). And then we stop again at the beginning of my question.
Just to make it clear:
If someone offers a solution that will cost money, such as a paid API, tool, or something similar, unfortunately this option is not feasible for me. I'm sorry :/
In a similar way, I know it would be ideal to do this processing with JavaScript (client-side). It turns out that I do not have a specialized JavaScript team, so this needs to be done on the server side (unfortunately), which is written in Java. I can only use a JavaScript solution if it is already ready, easy and quick to use. Would you know of any ready-made, easy-to-use library that can do it in a simple way? Does it exist?
You can't use a regular expression to parse HTML. See this question (which includes this rather epic answer as well as several other interesting answers) for more information, but HTML isn't a regular language because it has a recursive structure.
Any language that allows recursion isn't regular by definition, so you can't parse it with a regex.
Keep in mind that HTML is a context-free languages (or, at least, pretty close to context-free). See also the Chomsky hierarchy.

How to format the content of a textfield while typing

How can I format the content of a text field while the user is typing. Specifically, I want to add thousand separators to numbers.
(What to do in case of invalid/non-numeric input? Ideally, the formatting would simply not be applied. But it would also be an acceptable solution to me to prevent invalid input in the first place - if there is a good way to do that!)
I've searched for a solution of this problem for a long time and tried lots of examples, but none of the existing answers (e.g. Question about JTextField and text format with numbers?) are satisfying.
Thanks a lot!
Java's controls don't do this very well by default. I recommend using something like ImprovedFormattedTextField, which is documented very well in this post:
Is there any way to accept only numeric values in a JTextField?
Your question is not exactly a duplicate, but you should be able to adapt the class to work as you expect. The background will turn red if the number is not formatted correctly.

Introduce Delay after keyReleased() event

So, I'm working with swing and I need to find a clean (non-CPU-hogging-way) to introduce a delay on a text field. Basically, users will enter a number into this field and the keyReleased() event checks that the input fits a few parameters and then assigns the value to a data storage element in the program. If the data is invalid, it displays a message. Since the routine is called every time they type a letter (unless they type VERY fast), the input process becomes quite annoying (as in general one or two characters of data are not going to fit the allowed parameters).
I've tried setting up a timer object and a timer task for it, however it doesn't seem to work very well (because it delays the thread the program is running on). The option to just wait until the data reaches a certain length is also not possible since (as state before) the input can vary in length.
Anyone got any ideas? Thanks!
I've done stuff like this fairly frequently, and I have two suggestions.
The standard way of dealing with this is to use the InputVerifier class. This however only operates when the input component loses focus - it's designed to prevent the user navigating out of an input field when it's invalid, but it doesn't check until then.
The other way I've done this is to check validity on every keystroke, but not to bring up a message when it's invalid. Instead use a color to indicate validity - e.g. color it red when its invalid and black when valid. This isn't nearly as intrusive as the message. You can use a tooltip to give more detailed feedback.
You can also combine these methods.
Write a custom DocumentFilter. Read the section from the Swing tutorial on Text Component Features for more information.

How to filter out all non-word characters from user input in Java

I'm trying to filter out all possibilities of XSS from user name input while still allowing for foreign names to be inputted.
What is the best way to white-list all word-characters in multiple languages (also Chinese and Japanese and Russian)?
Is this possible at all? It would be easy to create a blacklist for XSS with "<>><" but then the hackers could work around this.
I think you may be approaching the problem from the wrong direction.
Typically, preventing XSS vulns is a case of ensuring that any user-generated content that you display on your website is properly escaped.
That way you ensure that what is displayed is exactly what the user entered, without having the risk of your whitelist inadvertently letting a few bad cases through.

Selenium typeKeys strips out dot from the String being typed

The following instruction
Selenium.typeKeys("location", "gmail.com");
types the string gmailcom instead of gmail.com.
What's happening there?
From the comments:
I am trying to simulate autofill and the only way to do it currently on selenium is to combine type and typeKeys. eg:
selenium.type("assigned_to", split[0]+"#");
selenium.typeKeys("assigned_to", "gmail.com");
Now my question is why typeKeys doesn't type the 'dot' in between gmail.com?
Have you tried using the Native key functions and javascript char codes?
I couldn't get a 'period' character to work using them (char 190), but I got the decimal (char 110) to work just fine, and the text box shouldn't have a problem with either.
selenium.Focus("assigned_to");
selenium.Type("assigned_to", split[0]+"#");
selenium.TypeKeys("assigned_to", "gmail");
selenium.KeyPressNative("110");
selenium.TypeKeys("assigned_to", "com");
Use the type method.
From the javadoc for typekeys:
this command may or may not have any
visible effect, even in cases where
typing keys would normally have a
visible effect
...
In some cases, you may
need to use the simple "type" command
to set the value of the field and then
the "typeKeys" command to send the
keystroke events corresponding to what
you just typed.
We had similar problems using typekeys in selenium python.
One workaround we figured to resolve this issue is to use the combination of 'type' and 'type_keys'. As you might be aware, type does not have such issues.
We did this in our selenium python script and it works just fine.
For example:
If there's an email address to be entered in a text box: test.me#test.me.uk
Then do
type(locator,’test.me#test.me.’)
type_keys(locator,’uk’)
Maybe a very crude way to do, but it did the job.
Hope this helps someone else with a similar problem.
Also try to set focus on element before write on it.
selenium.focus(locator);
selenium.typeKeys(locator, value);
it did function in my case, handling a input type=password.
Suppose the string to be typed using typeKeys is "abc.xyz.efg". Then, we can use type and typeKeys commands to write the given string.
type(locator,"abc.xyz.")
typeKeys(locator,"efg")
The above two steps are useful whenever you want to select an element in drop down box, and the drop down pops down only if we use typeKeys command.
I'm also seeing this behaviour when using Selenium RC (C#), and with different characters ('y' for example which also seems to remove the character follow it from the typing action..)
For some situations it is entirely possible to get around the issue by typing out the keys with the TypeKeys or KeyPress functions, but I haven't managed to find a solution that works when you want to type text in a field, enter control characters ([Enter] or a newline for example), and then type more text.. (using the 'Type' function in this case simply overwrites the field contents...).
If anyone manages to find a reasonable solution to this issue, please add it to this question as it's starting to come up in google now and would probably help alot of people out.. (I'd start a bounty if I could..)
This is an open bug in Selenium (bug SEL-519).
After some fiddling around with it, I finally managed to enter '.' into a textarea by using JavaScript. Execute something like window.document.getElementById('assigned_to').value += '.' via storeEval or the like.
I got the same behaviour but fixed it by passing a variable to the Type command instead of a string.
string email = #"name#gmail.com";
selenium.Type(inputfield, email);
It works like a charm!

Categories

Resources