Replace underscores on typing input in JavaScript - javascript

I have a text field with little dashes/underscores as shown in the image. How do I use JavaScript/backbone.js to make these dashes to be replaced by the input number upon typing?
As I type, I want the underscore to be replaced with the number I type. How do I achieve this?
Thanks in advance
input invoice field

Related

JSON is invalid when I take the value from HTML textarea when the user inputs text with multiple lines

So I have this json format of
{
questionID: int
studentAnswer: "String"
}
The studentAnswer field is populated from whatever is inputted in an HTML textarea. If I input something in one line only, the json is valid. But if I input something where i press enter to go to the next line, the json becomes invalid. I need the studentAnswer to be able to hold the input value with multiple lines. How can I fix this?
The problem is that multiline json's aren't valid. Since you're using a textarea, perhaps you can resize the textarea, restrict it, sanitize its inputs, or change the form type.
Ultimately, you want whatever the user input to be one line. Try this option out:
Is it possible to disable textarea's multiline option?

Regular expression ng-pattern angularjs

I have to validate an input field, but I have problems when the user copy and paste something inside the input
This is my code
<input type="text" ng-change="calculate()" ng-pattern="coordsPattern" ng-model="from" class="input-coords" placeholder="(x|y)">
where the coordsPattern is:
$scope.coordsPattern = /^\(?\-?\d{1,3}\|\-?\d{1,3}\)?$/;
the input can take
(158|158)
-158|158
(-158|158
.....etc
but when the user copy and paste the same thing from a different page, depending on browser to browser, the input looks like (158|158) but the pattern is invalid because when copying there are hidden tabs or spaces between chars. for example
((tab)(tab)158(tab)|(tab)(tab)-158(tab)
but in the input text looks like (158|-158 so for the user is a valid input
the input is valid (because in the calculate() function I clean the input from spaces and tabs) but invalid with that pattern and angular doesn't execute the calculate() function.
this one is a copy&paste text which includes hidden tabs
(‭-‭91‬‬|‭-‭18‬‬)
Thank you
EDIT
this is the var_dump of the string
string '(‭-‭91‬‬|‭-‭18‬‬)' (length=33)
it contains special chars! neither tabs or spaces!
maybe I have to find a different solution to validate the input...
$scope.coordsPattern = /^\s*?\(?\s*?\-?\s*?\d{1,3}\s*?\|\s*?\-?\s*?\d{1,3}\s*?\)?\s*?$/;
This should match the expected input even when there are whitespace characters inserted.

javascript/jquery input validation

I have form where a user can add more input boxes on button click.
User can have as much input boxes as they want.
I do not plan to add a button for removing fields.
They default number of input boxes is 2.
Say the user decides to add 3 more, now there are a total of 5.
For validation, I would like to check if the input box is empty or if the input has all spaces like: " " no matter how many spaces as long as it has nothing else but space.
I can do the check for an empty input by checking length, but how can I check for the latter?
Is there a regular expression for any number of consecutive spaces?
Thanks!
PS: I am using jQuery with jQuery mobile
You can check if an input field is blank by checking its .value.length, as you already know. To check if it only contains whitespace, then try this: (assuming that the input is stored in a variable called input)
if (!input.value.trim().length) // oh noes! it's blank or whitespace-filled!
Reference.
Your question has a few components:
how to add input fields dynamically?
how to loop through these fields and validate them as well?
how to check whether a field really contains content, not just empty values?
We need to address all of these issues in a systematic manner:
Starting with the easiest - detecting empty string:
if (value.replace(/\s/g,'')=='') //string is empty
Next, to add input fields dynamically:
var myinput=document.createElement('input');
document.body.appendChild(myinput);
//the trick here is to "remember" this element for later use
document.myinputs=[];
document.myinputs.push(myinput);
To check all your input fields, you check the static ones first, then loop through the dynamic input fields:
valid=true; //default to true unless detected otherwise
for (var i=0;i<document.myinputs.length;i++){
var input=document.myinputs[i];
if (input.value.replace(/\s/g,'')=='') valid=false;
}
alert(valid);

PHONE VALIDATION: How To Overwrite Text In a Textbox When Typing Without Clearing Whole Field?

I would like to know how to create a script that overwrites text that is already in a text box. (in jquery or javascript)
Example: If I have a text phone field that says:
+1 (xxx) xxx-xxxx
When a user clicks the field, I want the characters to remain, and the focus set to the 4TH character in the text box, just after the 1.
Then, as a user types each number, it overwrites the x one by one, until all the x's are gone. But, I want the parenthesis and hyphen formatting to stay, so the users input forms around the formatting.
I would also like this form to only allow numbers, hyphens, and parenthesis, and not allow submitting if x's still exist.
If you can help me with this, THANK YOU! :-)
Try masked input plugin # [
http://digitalbush.com/projects/masked-input-plugin/
]1
Looks like it matches what you need.

Allow only certain characters

I'd like to block all characters from being inputed except 0-9,a-z,A-Z range only alphanumeric characters. So when someone types ! for examplee nothing is written into input. How can I do that?
You need to write a function that listens for the onkeypress event for the form field, then check to see if the form contains any unwanted characters, and if it does, you update the field with those characters removed.
Or maybe you can use Alphanumeric Plugin for jQuery :
$('#yourInput').alphanum();

Categories

Resources