limit textarea entry without maxlength attribute in angularjs - angularjs

I have a requirement where the user can enter/copy-paste 25 ids separated by comma in a textarea ; there is no maximum size for the ids.Thereafter he cannot enter any characters.He then submits those ids for search.
Is there any way to limit the id number to 25 and then not allowing the user to enter anything.
I cannot use maxlength here.Also all these checking have to happen before user submits and the control reaches the angularjs controller.
I am using angularjs,HTML5,CSS3,bootstrap.

Basically what you'd need to is count the number of commas in the string and raise an error when it exceeds 24. You could do this using ng-onchange or when the user submits. This answer: Count the number of occurrences of a character in a string in Javascript should help you with counting the commas.

You can split the model of the input on ng-change like this for example ( where textareaValue is the model of the textarea ):
var textareaValueArray = textareaValue.split(',');
if( textareaValueArray.length > 25 )
textareaValue = textareaValueArray.slice(0,25).join(',')

Related

How do I allow a "SPACE" character in an AutoComplete JavaFX uneditable ComboBox?

When I type a SPACE character in an AutoComplete ComboBox, I can get the space character to be accepted except the addEventFilter code I'm using to manage it multiplies and inserts a space for each character previously typed prior to the space. You can see a screen shot example below where 3 spaces were added after the 3 characters (ive), then 4 spaces added after I include an additional charater (t), each after typing a single SPACE, and the spaces only appear after I type the next character (e.g. 'm').
I did try this with the ContolsFX AutoComplete, but it cannot handle the uneditable ComboBox - and couldn't find anything to the contrary. In the online cases I research, it was recommended to use the ComboBox's popup skin - addEventFilter to manage the SPACE character event. In nearly all the cases it was to consume() and prevent the space from selection and closing. I did not find anything that strictly allowed the space to be entered. I've tried adding the SPACE in code prior to and after this Event Code but the addEventFilter event.consume() will remove it. The SPACE character will only appear if I manage its addition within the addEventFilter method. I've tried different events such as KeyEvent.ANY, KeyEvent.KEY_TYPED, and KeyEvent.KEY_RELEASE and read the documentation on the KeyEvent, but only KeyEvent.KEY_PRESSED seems to allow the SPACE character, it just multiplies the number of spaces, and doesn't insert until the next text character.
ComboBoxListViewSkin cbSkin = cbSkin = new ComboBoxListViewSkin(cmb);
// cmb is the ComboBox
cbSkin.getPopupContent().addEventFilter(KeyEvent.KEY_PRESSED, (event) -> {
if(event.getCode() == KeyCode.SPACE){
filter += " ";
event.consume();}
});
I was able to solve my problem. The event code needed to be a part of the ComboBoxAutoComplete constructor and not part of the onKeyPressed event.
private ComboBoxListViewSkin cbSkin;
public ComboBoxAutoComplete(ComboBox<T> cmb) {
this.cmb = cmb;
cbSkin = new ComboBoxListViewSkin(cmb);
originalItems = FXCollections.observableArrayList(cmb.getItems());
cmb.setOnKeyPressed(this::handleOnKeyPressed);
cmb.setOnHidden(this::handleOnHiding);
cmb.setSkin(cbSkin);
cbSkin.getPopupContent().addEventFilter(KeyEvent.KEY_PRESSED, (event) -> {
if(event.getCode() == KeyCode.SPACE){
filter += " ";
event.consume();}
});
}

how to write script on validating number from the text? for ex: i like to get numeric value "2" from field "Staus(2)"

On Selenium, I'm writing script to get the number from the text. suppose there is a field 'Status(2)'. The number in the brackets keep changing. I want to get the value.
This code should get back the text for the element you have provided:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
Then you can have a look at this answer for how to use regex to extract the digit from the string: Regex to extract digit from string in Java
Hope this helps!
Here is the solution.
String rawText = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects")).getText();
String number = rawText.substring(s.indexOf("(") + 1).substring(0, s.indexOf(")"));
System.out.println(number);

clear text field using DELETE or BACK SPACE key in webdriver

I am trying to clear a text field using this action:
emailField.sendKeys("gmail.com");
emailField.sendKeys(Keys.CONTROL,"a",Keys.DELETE);
In above code, the last line only selects the text, does not delete it, but if I separate the actions it works.
emailField.sendKeys(Keys.CONTROL,"a");
emailField.sendKeys(Keys.DELETE);
From the JavaDoc for WebElement.clear():
If this element is a text entry element, this will clear the value.
Has no effect on other elements. Text entry elements are INPUT and
TEXTAREA elements. Note that the events fired by this event may not be
as you'd expect. In particular, we don't fire any keyboard or mouse
events. If you want to ensure keyboard events are fired, consider
using something like sendKeys(CharSequence) with the backspace key. To
ensure you get a change event, consider following with a call to
sendKeys(CharSequence) with the tab key.
Most likely you simply need to call:
emailField.sendKeys("gmail.com");
emailField.clear();
But if you need the clearing to be done via the keyboard for some reason, use Keys.BACKSPACE.
keys.DELETE can not work to delete the input text,you should use keys.BACKSPACE.
emailField.sendKeys(Keys.BACKSPACE)
From the JavaDoc for Keys.chord
chord(java.lang.CharSequence... value)
Simulate pressing many keys at once in a "chord".
You should be able to use
emailField.sendKeys(Keys.chord(Keys.CONTROL,"a",Keys.DELETE));
Tested in chrome driver
WE.send_keys(' \b')
This will add space then delete it (backspace)
I use in javascript and it's working fine:
await textBox.sendKeys(value);
await textBox.sendKeys(Key.BACK_SPACE);
emailField.sendKeys(Keys.BACKSPACE)
doesn't worked for me .
I used 'Key' instead of 'Keys'
emailField.sendKeys(protractor.Key.BACKSPACE)
emailField.sendKeys(Keys.CONTROL + "a",Keys.DELETE);
In PHP:
if you use php-webdriver (https://github.com/php-webdriver/php-webdriver) you must:
use Facebook\WebDriver\WebDriverKeys AS Keys;
.
.
.
$this->driver->findElement(By::id('demo'))->sendKeys([Keys::BACKSPACE,'Any other text']);
Just adding another working C# example using the Google Chrome webdriver.
SendKeys only takes one parameter so created a string with the Crtl + A. This code sequence will select the current text in the field then delete the text.
Code example:
var crtlA = Keys.Control + "a";
driver.FindElement(By.XPath("//div[3]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/span/input")).SendKeys(crtlA); Wait(5000); // Select current text
driver.FindElement(By.XPath("//div[3]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/span/input")).SendKeys(Keys.Delete); Wait(5000); // Clear current text
driver.FindElement(By.XPath("//div[3]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/span/input")).SendKeys(newItemSku); Wait(5000); // Input SKU name
1. in WebdriverIO, i tried to edit the text by clear text (which contains special charactes like #, +, _) in text field by below following step. Eventhough it was not successful.
example: text=> abc+1234#gmail.com
step1:browser.clearElement(selector);
step2:browser.execute(function () {
document.querySelector(>>>Cssselector<<<).value="";
});
step3: browser.doubleClick(selector);
browser.keys("Delete");
step4: browser.click(selector);
browser.keys(['Meta',a]);
browser.keys('Meta');
browser.keys('Delete');
Note: below step is resolved this issue.
var count= browser.getAttribute(selector, value).length;
for (var i=0;i<count;i++)
{
if (browser.getAttribute(selector, value)=='')
break;
}
else
{
browser.doubleClick(selector);
browser.keys("Delete");
}
browser.pause(200);
// it will clear your text field easily.
Note:
You can add the new text now.

AngularJS Get words with a prefix from text box

I am trying to get words with a certain prefix when a user types into a text box. For example, let's say I want all words in a text box that begins with "#". How would I go about getting these words?
One way to do it is to use the JavaScript split() method.
Let's say you get the value of a user input into one of your variables. You can use this method to get an array of String of the words that start with the prefix you chose.
var input = "#This is what the #user typed";
var splitArray = input.split("#");
// splitArray = ["This is what the", "user typed"]

AngularJS Not counting newlines in var length

When I enter some text and then enter some new lines using test.length does not count new lines in the string. I'm using this text to send SMS messages which are sensitive to all newlines so it all has to count. Any ideas?
Hit enter 4 times and see the counter doesn't go up.
http://codepen.io/clouddueling/pen/HJAfn
You need an ng-trim="false" to avoid automatic trimming: http://codepen.io/musically_ut/pen/KHBto
The documentation for this is missing but there is an pull request on the way.
The control characters for CRLF (\r\n) typically don't count in the length of a string. You can detect them with a matching regex and use the number of matches to increment your character count. Something like this:
var crlfCount = mytext.match(/[\n\r]|[\r\n]/g);
linecount += crlfCount.length;

Resources