How to trim all characters and numbers from INPUT? - javascript

so i have this function that displays the inputted value of the user. I want to erase them all after clicking a specific button.
Here's the code:
HTML
<center> Are you sure you want to delete all?<br><br></center>
<div id="options11" onclick="EraseMe()"> Yes</div>
<div id="options12" onclick="CloseButton()">No</div>
JS:
function List(){
document.getElementById('order').innerHTML += document.getElementById('Name').value + document.getElementById('quan').value + parseInt(document.getElementById('Total').value);} //gets the inputted value of the user
function EraseMe(){
var name = document.getElementById('Name').value;
var quan = document.getElementById('quan').value;
var totals = document.getElementById('Total').value;
name.replace(/^[a-zA-Z0-9]$/, "");
quan.replace(/^[a-zA-Z0-9]$/, "");
totals.replace(/^[a-zA-Z0-9]$/, "");}
and also, i want to sum up all the value of the document.getElementById('Total') , but the result I'm having is a value of a string, not as an integer.
Here's the code for that:
function compute(){
totall = document.getElementById('quan') * document.getElementById('price');
document.getElementById('totalsss').value += parseInt(totall);
document.getElementById('totalsss').innerHTML = document.getElementById('totalsss').value;}

One problem, is that replace is a pure function and does not modify the input - as such, the result of String.replace must be used.
Another is the regular expression is anchored (meaning it would only match input of a single character in the given case), and yet another is the regular expression is not global (meaning it would only match once) ..
Try:
var nameElm = document.getElementById('Name');
// assign replacement result back to input's value,
// after substituting all (g) English alphabet characters/numbers
// with nothing .. any other characters will remain
nameElm.value = nameElm.value.replace(/[a-zA-Z0-9]/g, "");

If you want to blank something, just replace it with an empty string:
function EraseMe(){
document.getElementById('Name').value = "";
document.getElementById('quan').value = "";
document.getElementById('Total').value = "";
}
There's no reason to do that regex replacement unless there's something you want to keep.

Related

Can Anyone explain how to identify the match text in jquery?

Assume that newFile[0] = 'Dany' and prsntFile[0] = 'Dany'. If newFile equal to present file then alert will work. My question is some time prsntFile[0] = 'Dany - 1' and my newFile[0] = 'Dany' but for this also i want to trigger the alert function.
prsntFile[0] may also contain dany -2 or dany -3. for this also i want to alert 'match'. any solution guys*
var newFile = 'Dany';
$(".copyDoc").each(function(){
var existFiles = this.value;
var prsntFile = existFiles.split('.');
if(newFile[0] == prsntFile[0]){
alert('match');
}elseif(newFile[0] == prsntFile[0]){
}
//alert(newFile[0]);
});
My Input hidden field
<input class="copyDoc" name="copyDoc[]" value="Dany - 1" type="hidden">
You can use, regex to do it.
var a = 'Dany'
var b = 'Dany 2'
var c = 'Day2'
var d = 'Dany-2'
var patt = new RegExp("^"+a+"");
console.log(patt.test(b));
console.log(patt.test(c));
console.log(patt.test(d));
The test() method tests for a match in a string.
This method returns true if it finds a match, otherwise it returns false.
So, I have created a regular expression object, patt and used that with test to find if there is a match in the string.
In regex : ^n Matches any string with n at the beginning of it

How to find symbols on a page and get a values between the two symbols in JS?

Some page have inserted secret text and JS script on this page should get a value between two characters, for example, between characters ##. In this variant ##569076## - 569076 should receive.
Here is what i've tried:
<div id="textDiv"></div>
<script>
var markup = document.documentElement.outerHTML;
var div = document.getElementById("textDiv");
div.textContent = markup.match(/##([^#]*)##/);
var text = div.textContent;
</script>
But nothing displayed
The problem is that the match function returns an array of matched values and textContent will only accept a string.
So you have to select which array item you want to use for the textContent assignment:
div.textContent = markup.match(/##(\d+)##/)[1];
Note we selected the first captured item using the [1] from the matches array.
If the string has spaces in, just add a space in the list of matched items like so:
var markup = '##56 90 76##';
alert( markup.match(/##([\d ]*)##/)[1]);
you should put your text/code into a variable.
You can replace body selector by yours.
Plain JS:
var html = document.getElementsByTagName('body')[0].innerHtml,
match = html.match(/##(\d+)##/),
digits;
if (match) {
digits = match[1];
}
if you are using jQuery and like shortands:
var digits, match;
if (match = $('body').html().match(/##(\d+)##/)) {
digits = match[1];
}

How to capitalize first letter of every single word in a text area?

i have a multiline textfield("Adressfeld"), and i want to Uppercase every first letter and LowerCase the rest of every single word in this text area.
Here is my try:
function capitalize(Eingabe){
Eingabe = this.getField("Adressfeld").value;
var strArr = Eingabe.split(" ");
var newArr = [];
for(var i = 0 ; i < strArr.length ; i++ ){
var FirstLetter = strArr[i].charAt(0).toUpperCase();
var restOfWord = strArr[i].slice(1).toLowerCAse();
newArr[i] = FirstLetter + restOfWord;
}
return newArr.join(' ');
}
Ausgabe = this.getField("Empfängername");
Ausgabe.value = capitalize();
With the script shown above, every single word in the first line of the text area is capitalized. But in every other line, the first word isn't capitalized.
How i have to change the script to get it work?
Thanks,
I have included an example below try like that, it will solve your problem
Html
<input type="button" value="clk" onclick="z();"/>
<textarea rows="4" id="text" cols="50">
JS
function z()
{
var z=document.getElementById("text").value;
var x=z.replace(/\b./g, function(m){ return m.toUpperCase(); });
alert(x);
}
DEMO
I you want to Convert Every first letter of each word to upper and all other letters are lower then first convert the entire string to lowercase.Then do the same things as above.
DEMO2
Meinst du sowas?
var textToArray = document.getElementById('myTextarea').value.split('\n');
/* Hier eine Schleife zur Bearbeitung von textToArray */
var arrayToString = textToArray.join(' ');
The split operation fails -- the result of the first split cannot be split again on another character.
I see no reason to first replace returns by a single type \n and then try to split on either \n or a space. It's way easier to just replace the returns by a single space and only then split:
var strArr = String(Eingabe.value).replace(/[\r\n]+/g," ").split(" ");
With that, the rest seems to work.
Here is another approach, which may or may not work as well (it depends on whether Javascript's interpretation of "word boundary" \b agrees with your own):
function capitalize(Eingabe){
// Eingabe = this.getField("Adressfeld");
var strArr = String(Eingabe.value).replace(/[\r\n ]+/g,' ');
strArr = strArr.replace (/\b[a-z]/g, function(found) { return found.toUpperCase(); });
return strArr;
}
A few things:
• The argument of the function is Eingabe. In this case, this is a variable containing a value, and it does not make sense at all to redefine in the first line of the function. The way the function looks like, you won't need an argument, and therefore, your function definition looks like this:
function capitalize() {
• That done, define Eingabe properly as var Eingabe .
• With the array, you essentially want to create a two-dimensional array. First create the array of lines and then a loop through the lines, as it has been suggested in the answer by #Entimon
• You end with
this.getField("Adressfeld").value = capitalize() ;
And that should do it.
thanks for your help!
The correct answer is based on Arunprasanth KV's jsfiddle.net/svh1jd99
function capitalize()
{
var capitalize = this.getField("Adressfeld").value;
var done = capitalize.replace(/\b./g, function(m){ return m.toUpperCase();});
return done;
};
this.getField("Adressfeld").value = capitalize();
Thanks again for your help.
By using jQuery you can do this as shown below:
Demo: https://jsfiddle.net/cxow8198/3/
<input type="text" id="input">
<script>
//usage
$("input").keyup(function() {
toUpper(this);
});
//function
function toUpper(obj) {
var mystring = obj.value;
var sp = mystring.split(' ');
var wl=0;
var f ,r;
var word = new Array();
for (i = 0 ; i < sp.length ; i ++ ) {
f = sp[i].substring(0,1).toUpperCase();
r = sp[i].substring(1).toLowerCase();
word[i] = f+r;
}
newstring = word.join(' ');
obj.value = newstring;
return true;
}
</script>
Query code snippet to make capitals of the first letter of every word in the string. This could be used to prevent users from entering all caps for titles or text when they input data into forms.

Incrementing a number in a string

I have the following string: 0-3-terms and I need to increment the 3 by 20 every time I click a button, also the start value might not always be 3 but I'll use it in this example..
I managed to do this using substring but it was so messy, I'd rather see if it's possible using Regex but I'm not good with Regex. So far I got here, I thought I would use the two hyphens to find the number I need to increment.
var str = '0-3-terms';
var patt = /0-[0-9]+-/;
var match = str.match(patt)[0];
//output ["0-3-"]
How can I increment the number 3 by 20 and insert it back in to the str, so I get:
0-23-terms, 0-43-terms, 0-63-terms etc.
You're doing a replacement. So use .replace.
var str = '0-3-terms';
var patt = /-(\d+)-/;
var result = str.replace(patt,function(_,n) {return "-"+(+n+20)+"-";});
Another option is to use .split instead of regex, if you prefer. That would look like this:
var str = '0-3-terms';
var split = str.split('-');
split[1] = +split[1] + 20;
var result = split.join('-');
alert(result);
I don't understand why you are using regex. Simply store the value and create string when the button is called..
//first value
var value = 3;
var str = '0-3-terms';
//after clicking the button
value = value+20;
str = "0-" + value + "-terms"

using substring to clear the last letter of a string in javascript

I have a textarea with the id display_main.
I need this backspacing function to clear the last letter or number of the text area.
This code below did not work.
Please explain.
function backspacing(){
document.getElementById("display_main").substring(0, display_main.length - 1);
}
document.getElementById returns an element. Elements aren’t strings, so it doesn’t make sense to take substrings of them. You’ll need to use its value property, which is the text contained in the textarea (and many other input elements):
function backspacing() {
var displayMain = document.getElementById("display_main");
displayMain.value.substring(0, displayMain.value.length - 1)
}
Also, substring doesn’t modify the string it’s called on; strings are immutable. It returns a new string, which you need to assign to something:
function backspacing() {
var displayMain = document.getElementById("display_main");
displayMain.value = displayMain.value.substring(0, displayMain.value.length - 1);
}
Because you're trying to substring the element, instead of the element's value
var element = document.getElementById("display_main");
element.value = element.value.substring(0, element.value.length - 1);
Should be:
function backspacing(){
var textarea = document.getElementById("display_main");
textarea.value = textarea.value.substring(0, textarea.value.length - 1);
}
get first text area value and apply substring function.
textAreaValue = document.getElementById("display_main").value
then apply substring function on value then again set modify value to textarea.
You can clear the last character of the textbox and assign it to itself with this code
function backspacing() {
var textbox = document.getElementById("display_main");
textbox.value = textbox.value.slice(0,-1) ;
}
Or if you have jQuery library loaded you can use :
function backspacing() {
$("#display_main").val($("#display_main").val().slice(0,-1));
}

Categories