Convert CharCode to Char? - apache-flex

What I need
ok I googled this and there are many tutorials on how to get the charCode from the character but I cant seem to find out how to get the character from the charcode.
Basically I am I am listening for the KeyDown event on a TextInput.
I prevent the char from being typed via event.preventDefault();
Later I need to add the text-char to the TextInput.
I can get the charCode via event.charCode so if I can turn that into a string I can save it for later user.
Why I need it
Basically I am making a TextInput, that that I can set to display default text in it. When A user types into it, I want to remove the default text first then add the user typed text.
Currently I am either removing it all, or ending up with both.

It's simple:
var yourNewChar:String = String.fromCharCode(event.charCode);

Related

Qt LineEdit setInputMask() with setText() and QRegExp

I have a QLineEdit for Date in mm/dd/yyyy format. I am getting input using the keyboard and not using QDateEdit because of the requirement. And when the lineEdit comes to view, it has to show to the user the current date. I need the following for the lineEdit.
I need the two slashes always to be displayed and the cursor has to skip while entering or deleting.
I should not allow the user to enter an invalid date i.e while entering itself the lineEdit should not get invalid numbers.
I have to set the current date as the default text when the lineEdit comes to view.
For the first point, I tried using setInputMask("99/99/9999") but with this I can't set the current date using setText(). And how to use QRegExp to not to allow lineEdit get an invalid number while employing setInputMask()?
QDateEdit will serve your purpose.
use setDisplayFormat("dd/MM/yyyy").
QDateEdit wont allow invalid dates
You can use QDateEdit::setDate() obtained from
QDateTime::currentDateTime()
For setting text into QLineEdit with setInputMask("99/99/9999") you should format text depending on your mask:
lineEdit.setText("{:02d}/{:02d}/{:04d}".format(m, d, y))
Alternatively, you can temporary disable InputMask, format your date without /, set it and re-enable InputMask. But make sure that number of symbols in every part is correct.
lineEdit.setInputMask("")
lineEdit.setText(date_str.replace("/", ""))
lineEdit.setInputMask("99/99/9999")

How do I add validation to a cell in the Ext Lib datagrid?

I have a datagrid that contains data from different documents. The user can edit some of the columns. I want to restrict them to only be able to enter a number.
I would like to do it from the client side instead of server side as that would mean checking 20 or more documents.
ok figured out what to do. Create a function to format the data with as red background if they enter a non-numeric or invalid value. Put the function in a scriptBlock and put the name in the formatter field for each column
function ValidNmbr(s)
{
var RegularExpression = new RegExp(/^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/);<br/>
if(RegularExpression.test(s))
{
return s;
}
else {
return "<span style='background-color:red'>"+s+"</span>";
}
}
Client side format enforcement can be bypassed (anyone having firebug), so you have to be clear it is only for the comfort of the user, not for the integrity of your data.
On the server side: you can have a entry field with a number mask. No code required -- might be the least work. If you want to do that client side:
use the HTML5 attributes for number format
use some helper to make older browsers behave
consider to use a Dojo grid. It does nice validation
Hope that helps

Reading integer from gridview textbox

I am converting a string that is being read from a textbox in gridview
int numTC = Convert.ToInt32(((TextBox)row.FindControl("numTC")).Text);
However it is returning the following exception:
Input string was not in a correct format.
Can anyone see anything wrong in the conversion?
Thanks
Make Sure that your gridview can accept only numbers you can have a filterextender using ajax and I m sure u will do that what else you can do is to check whether you have a textbox is null or not using the Function given below
if(string.IsNullOrEmpty(((TextBox)Row.FindControl("numTC")).Text))
{}
((TextBox)GridViewname.Rows[e.RowIndex].FindControl("numTC")).Text;
and
use this extender or u can use javascript as well
If it is going inside the if statement that means the value is null
if(!string.IsNullOrEmpty(((TextBox)row.FindControl("numTC")).Text)) {}
I have used ! sign now it will go inside the if statement if there is some value in it.
and try to convert this text into integer using try catch block if u get any exception you can take whatever action you want to.
Let me know if it is complete
It is obvious that the value of the returned in the "Text" property of the text box cannot be converted to inter, I guess you have to insure first that you are returning the correct textbox and that it contains a valid value before attempting the conversion.

Asp.NET Filtered TextBox Extender - Invalid Char Sequence?

Is it possible to put a character sequence in a Filtered TextBox Extender in Asp.NET? My guess is no, but I'm curious. I want the user to be able to enter the characters (such as & and #), but not enter the invalid sequences (such as &#).
Why not just use a regular expression? Because when the form is submitted, all the fields are passed to the server...including fields that are not part of the validation group. These fields do not get checked and may trigger the "A potentially dangerous Request.Form value…”, aka the HttpRequestValidationException. And preventing that message is the whole point of this. I'd rather tell the user, with a regex validator, what they are doing wrong...but I will settle for preventing them from typing the bad chars (&#, <, >).
Edit: sort of an afterthought, but if there's a better way to prevent ALL TextBoxes from including the characters, that'd be great!
You can just have a function onkeydown for all your text boxes and that checks the character pressed and if it's invalid just remove that last character. Also for your combination strings you can just check to see if the previous characters make it invalid then remove all of them.
An example of that type of function is:
function checkKey(event) {
var code = event.keyCode;
// code is the ascii number of the key.
}

Find out the character pressed key

If I add a listener to KeyboardEvent.KEY_DOWN, I can find out the keyCode and the charCode.
The keyCode maps to a different character depending on the keyboard.
The charCode is just as useless, according to the help:
The character code values are English keyboard values. For example, if you press Shift+3, charCode is # on a Japanese keyboard, just as it is on an English keyboard.
So, how can I find out which character the user pressed?
You left out a pretty important part of the quote or it was missing where you found it:
For example, if you press Shift+3, the
getASCIICode() method returns # on a
Japanese keyboard, just as it does on
an English keyboard.
http://livedocs.adobe.com/flex/201/langref/flash/events/KeyboardEvent.html
This is probably more helpful:
The charCode property is the numeric value of that key in the current character set (the default character set is UTF-8, which supports ASCII).
http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000480.html
Your application determines what characters set is used, meaning that the even if you have to use separate keys of different keyboard locals to produce the same character, it will have the same charCode.
NOTE: (This is about keyboard messages in general and does not apply to actionscript alone. I misread the question and provided a deeper answer then was helpful)
Really, the path from keyboard to windows char is a VERY complex one, it goes something like this:
Keyboard send scancode to Keyboard device driver (KDD).
KDD sends a message to the system message queue.
The system then sends the message to the foreground thread that created the window with the current keyboard focus.
The thread's message loop picks up the message and figures out the correct character translation.
The 'real' char that was typed is not calculated until it finishes that whole process, as each window and thread can be on a different locale and you can't really 'translate' the key without knowing the locale and key buffer history.
The "WM_KEYDOWN" and "WM_KEYUP" messages cannot just be converted with MapVirtualKey or something because you don't know how many key presses make up a single char. The simple method is just handle the 'WM_CHAR' event and use that. Consider the following:
en-US locale, you press the following keys a + ' + a, you get the following output "a'a"
pt-BZ locale, you press the following keys a + ' + a, you get the following output "aá"
So in both examples you would get 3 KEYDOWN, KEYUP messages, but in the first you get 3 WM_CHAR and in the second you only get 2.
The following article is really good for the basic concepts:
http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
You cannot effectively use charCode or keyCode to determine the character that was entered. You must compare strings only. The KeyboardEvent does not give you the entered text, which is also silly.
In my case I implemented a KeyboardEvent.KEY_DOWN event in addition to a TextEvent.TEXT_INPUT event. In the handler for the latter I implemented all functionality where the charCode was needed and didn't vary per keyboard locale (eg. space bar or enter). In the the former I checked for the text property of the event to compare what I needed locale independent.
Forgot to mention that this post hinted me to that solution: How to find out the character pressed key in languages?
Typing Japanese hiragana etc characters often require several keystrokes and sometimes even selecting the appropriate character from a drop down menu. You probably want to listen for a different event, something like a textfield's change event.

Resources