How to lock a character in an input field - actionscript-3

I have an input text field in Flash and I want to keep a dollar sign at all times in front of it.
Normally I would just left align the input field and have a "$" next to it so it's uneditable, but in this instance the text field has to be center aligned.
I thought to just include a function to add "$" in front of all the text every time the field loses focus - but realised that would be a problem if there was already a $ sign in front and it just kept adding them.
Also - once I've done this, is there a way to grab the value from that input field excluding the "$"? Eg: some sort of splice that splices the first character and just grabs the rest of it.

To keep the $:
tf.addEventListener( Event.CHANGE, onTextChange );
function onTextChange( e:Event ):void
{
if ( tf.text.charAt(0) != "$" )
tf.text = "$" + tf.text;
}
And to get the text without the first character:
var yourText :String = tf.text.substring(1, tf.text.length);

Related

How to create an input field (HTML) that spans two lines

I want to be able to use an <input> field type of control but allow only two lines.
At the moment I am using two fields but was wondering if anyone can come up with a solution to allow input (similar to a textarea) but no more than two lines. I control the width etc of the field.
For reference, Jquery and Bootstrap 3 are loaded.
Any help much appreciated.
try this
var element = document.getElementById('tworows');
make2Lines(element);
function make2Lines(el){
el.setAttribute('rows', 2); // limit height to 2 rows
// el.setAttribute('wrap', 'off'); // ensure no softwrap is not required anymore if we limit the length
el.addEventListener('keydown', limit); // add listener everytime a key is pressed
function limit(e){
if(e.keyCode == 13 && this.value.indexOf('\n')>-1){
// 13 is the ENTER key and \n is the value it make in the textarea
// so if we already have a line break and it's the ENTER key, we prevent it
e.preventDefault();
}
// async to let the dom update before changin the value
setTimeout(limitRow.bind(this), 0);
}
function limitRow(){
var maxLength = 10;
var rows = this.value.split('\n');
rows.forEach(cutOverflow)
this.value = rows.join('\n');
function cutOverflow(row, index, rows) {
rows[index] = row.substring(0, maxLength);
// this if is only if you want to automatically jump to the next line
if (index === 0 && row.length > maxLength)
rows[1] = row.substring(maxLength) + (rows[1] || '');
}
}
}
<textarea id="tworows"></textarea>
short version : function make2Lines(a){function b(a){13==a.keyCode&&this.value.indexOf("\n")>-1&&a.preventDefault(),setTimeout(c.bind(this),0)}function c(){function c(b,c,d){d[c]=b.substring(0,a),0===c&&b.length>a&&(d[1]=b.substring(a)+(d[1]||""))}var a=10,b=this.value.split("\n");b.forEach(c),this.value=b.join("\n")}a.setAttribute("rows",2),a.addEventListener("keydown",b)}
Two ways come to mind:
You could use a <textarea> instead, and augment it with some script that only allows two lines.
You could continue to use two <input> fields, but style them so they stack on top of each other to create the illusion of one field. You might still need a bit of script to take care of some usability annoyances, such as pressing ENTER to go from line one to line two.
If you are talking about wrapping lines if the text is too long, according to documentation <input type="text"> cannot wrap text.
However, if you are talking about limiting the character length, you could use the maxlength attribute like- <input type="text" maxlength="10">
An input field can only display one line http://www.w3.org/TR/html-markup/input.text.html#input.text. For multiline you need to use textarea and set the rows attribute. If you need two separate values you can do it after in PHP, Javascript or other means.
<textarea class="form-control" rows="2">The default text or empty for nothing this is passed as value for this field</textarea>

add value to current value of text input in flash AS3?

I'm trying to add value to the current value of an input text field in AS3.
EXAMPLE: I have a few buttons and each button has a value, when i click on each button, the value of that button gets copied/inserted into a text input field on the stage.
further explanation:
button 1 value is (BALL)
button 2 value is (Book)
button 3 value is (Pen)
button 4 value is (cup)
etc etc ....
I have an empty input field on the stage called rest_Text.text.
so when I click on any of the buttons above, the value of that button gets copied inot the rest_Text.text...
and the final result would be something like this in the rest_Text.text:
BALL, Book, Pen
my current code is this:
function clipClick(e:Event):void {
MovieClip(root).main.loginHolder.rest_Text.text = e.target.clickTitle;
}
the code above will delete the current value and replaces it with a new one! but i need to add each value to the current one without deleting the old value.
any help would be appreciated.
Thanks in advance.
You can concatenate strings using the addition operator (+). For example:
trace(btn1.clickTitle + btn2.clickTitle + btn3.clickTitle);
//traces "BALLBookPen"
Adding on to an existing string is done with addition assignment (+=). Since you want a comma and space between each string, this is how you'd rewrite your function:
function clipClick(e:Event):void {
MovieClip(root).main.loginHolder.rest_Text.text += ", " + e.target.clickTitle;
}

AS3 TextField: Append text at a certain line?

I'm trying to find a way to append text (appendText) at a certain TextField line number.
I found a way to return the first character of a line:
tf.text.charAt(tf.getLineOffset(10)); //selects line 10
But I haven't found a way to append text. Any help would be appreciated!
This should do the trick (put the supplied text at the start of the supplied line), though there may be a more efficient way of doing it.
function prependToLine(textField:TextField, line:int, text:String):void {
var lineOffset:int = textField.getLineOffset(line-1);
textField.text = textField.text.substring(0,lineOffset) + text + textField.text.substr(lineOffset);
}

Retain newline when getting contenteditable div text

I wanted to save the text inside a contenteditable div being pre formatted. How would i get the pre form of the text and not the text where \n and \r are ommitted?
$('#save').click(function(e) {
var id = "board_code";
var ce = $("<pre />").html($("#" + id).html());
if ($.browser.webkit)
ce.find("div").replaceWith(function() { return "\n" + this.innerHTML; });
if ($.browser.msie)
ce.find("p").replaceWith(function() { return this.innerHTML + "<br>"; });
if ($.browser.mozilla || $.browser.opera || $.browser.msie)
ce.find("br").replaceWith("\n");
alert( ce.text() );
});
http://jsfiddle.net/AD5q7/10/ this doesnt work
try string for contenteditable div
UPDATE: try the string by typing it. There maybe no problem when the string is pasted.
1
abc def
gh i
jkl
2
#include<iostream.h>
#include<conio.h>
int main(){
int grade, passingMark=75;
cout<<"Hi there, please enter your mark: ";
cin>>grade;
if( ((grade >= passingMark)||(grade==35)) && (grade<101)){
cout<<"\nPass!";
}
return 0;//15lines
}
The save file must be also formatted like this and not without \n\r removed. Im expecting that the alert should include \n
When contenteaditable div looses focus, the entire text gets converted to html for eg
<div contenteditable="true">Your text is here
and has new line </div>
upon loosing focus it converts the virtual textarea to html i.e.
<div>Your text is here</div><br><div>and has new line </div>
and when you'll attempt .text(), you'll loose the desired alignment as actually the don't exist anymore in that div.
Solution
1. You can use textarea, with border properties set to 0 which would make it look like a contenteditable div or
2. You can grab the entire html of the contenteditable div and replace the html with the corresponding text representations using javascript (for that refer javascript regex replace html chars)
Try this
alert( ce.text().replace(/\n/g, "\\n" ).replace(/\r/g, "\\r"));

How to remove a particular text in TextInput in Flash?

Design a Text Input by using components in flash and its InstanceName is listChat. In that text input I added a text by Dynamically. While I am adding text it displays with null.
For Example I added "apple" it Displays like nullapple
How to remove that null?
You can use the String's replace method.
If you want to remove only the first encounter of 'null' you can use this:
listChat.text = listChat.text.replace("null", "");
If you want to remove all encounters of 'null' this will do it:
var stripNullPattern:RegExp = /null/gi;
listChat.text = listChat.text.replace(stripNullPattern, "");
If you only want to remove null if it's in the first four characters use something like this:
if(listChat.text.substring(0, 4) == "null")
{
listChat.text = listChat.text.replace("null", "");
}
Check the AS3 reference for more info:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#replace()