How to test if a string has only spaces in Javascript? - javascript

I have a textarea and I need to test if the user put a text like " ", or only spaces in it or only " ", I can't accept only spaces, but I can accept " Hi !!". How can I do this in Javascript?

Just trim it and the length will be 0 if it is all spaces.
strname.trim().length == 0

You check it like this: demo on JSexample
<script>
var text = ' '
if(text.match(/^\s*$/)){
alert('contains only spaces!')
}
</script>

Be careful, some browsers don't support trim() function. I'd use like this:
if (!!str.replace(/\s/g, '').length) {
alert('only spaces')
}

Related

jQuery escaping quotes

I'm trying to escape quotes in the text I need to change.
The problem is that if I have quotes in #stuff (" or ' in CHANGE1 or CHANGE2) it keeps failing my whole request. Otherwise if I escape it with escapeQuotes() it won't find " in my original text.
(function($) {
$.fn.escapeQuotes = function() {
return this.html().replace(/"/g, '"');
}
})(jQuery);
$("#stuff").each(function() {
$(this).html($(this).html().replace("CHANGE1", "CHANGE2")).escapeQuotes();
});
Let's say I have this in #stuff:
<div id="stuff">
I'm trying to escape this. ALSO I'D LIKE TO ESCAPE THIS: ".
</div>

How to put quotes around a getElementById quote + string?

I have been working on this assignment where I have to make a prompt in which the visitor puts types in a string (or just any sentence for this matter) and afterwards the typed text needs to be shown on the page itself alongside with an indication in what line the first spacebar is implemented.
The problem however is that i need to put this in quote signs and since I am using a "+ script" in my Code, I cannot put it inside quotes.
Here is the code I am using:
<body>
<p id="Result"> </p>
<p id="First Spacebar"></p>
<script>
let string = prompt("Put text here");
const text = string;
document.getElementById('Result').innerHTML = "You said: " + string
document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" "));
</script>
</body>
Everything is working like it should, but i can't seem to get the " signs on each end of the string in the webpage version.
You can use template strings
<body>
<p id="Result"> </p>
<p id="First Spacebar"></p>
<script>
let string = prompt("Put text here");
const text = string;
document.getElementById('Result').innerHTML = `You said: "${string}"`
document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" "));
</script>
</body>
You can either escape the " that are meant to be displayed ("..\""), or , you can enclose your strings in single quotes ' and freely use double quotes " inside it:
let string = prompt("Put text here");
const text = string;
document.getElementById('Result').innerHTML = 'You said: "' + string + '"';
document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" "));
<p id="Result"></p>
<p id="First Spacebar"></p>
With ES6 template literals you can do it like this:
`You said: "${string}"`
Old style, you can leverage the fact that you can use either " or ' as outer quotation marks:
'You said: "' + string + '"'
Here are 2 ways:
document.getElementById('Result').innerHTML = 'You said: "' + string + '"'
(this uses both kinds of quotation marks so one can be used inside strings)
document.getElementById('Result').innerHTML = "You said: \"" + string + "\""
(this uses the escape character, '`', to indicate quotation marks that should be used as normal characters instead of surrounding strings)
You can use javascript escape codes. You can find an easy conversion table for these escape codes here.
The javascript escape code for " is \u0022.
A full script example:
<body>
<p id="Result"> </p>
<p id="First Spacebar"></p>
<script>
let string = prompt("Put text here");
const text = string;
document.getElementById('Result').innerHTML = "You said: \u0022" + string + "\u0022";
document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" "));
</script>
</body>

Trim white space before text in javascript

Can't find the answer, I tried with what I found on this website and elsewhere but it never works.
Goal : to trim white spaces before and after a word giving in the field DIV.
Problem : everything works fine as long there is NO white space before a word ! There can already be text in the DIV of it can be an empty DIV where text can be inserted.
I tried in the javascript in the DIV like trim(this) but that doesn't work. Even inthe script itself I tried $.trim(editableObj.innerHTML) , nor editableObj.innerHTML.trim()
Nothing works !
JS
<script>
function showEdit(editableObj) {
$(editableObj).css("background", "#FFF");
}
function saveToDatabase(editableObj, column, linkmainid, nav, linkid) {
$(editableObj).css("background", "#FFF url(loaderIcon.gif) no-repeat right");
$.ajax({
url: 'save'+nav+'.php',
type: "POST",
data: 'linkcat=' + column + '&linktitle=' + editableObj.innerHTML + '&linkmainid=' + linkmainid + '&action=' + nav + '&linkid=' + linkid,
success: function(data) {
$(editableObj).css("background", "#FDFDFD");
window.location="/website/cms/menu.php";
}
});
}
HTML
<div class="navnew" contenteditable="true" onBlur="saveToDatabase(this,'1','999999','navnew','111')" onClick="showEdit(this);"></div>
var stringWithLeadingSpaces = " hello world!"
var trimmedString = stringWithLeadingSpaces.trim();
console.log(trimmedString); //logs "hello world!"
trim
Add this snippet to your code:
String.prototype.trim = String.prototype.trim || function trim() {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };
Then, use:
var myValue = " one, two, three ".trim();
or
var myValue = " one, two, three ";
var myValueTrimmed = myValue.trim();
Typing spaces into the content editable DIV inserts non breaking space characters. If you retrieve the content of editableObj.innerHTML these show up as sequences of the character entity string " ". Naturally String.prototype.trim does not remove occurrences of " " because they are not white space text!
If you retrieve the content as editableObj.textContent the text returned contains actual non-break spaces with character code 0xA0. String.prototype.trim does remove these.
Ultimately choose a trim method that suits your needs, depending on whether you want to save HTML or plain text in the database.

Javascript function parameter character escape

I need to pass a variable to a JavaScript function, but I have a little trouble. In the .cs file, I have written:
string id = "some'id";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
As you can see there is an ' (single quote) in the id. Is there any way to work around this issue?
Escape ' with a \ (backslash). For example,
console.log('string with \'');
Escape your string for such kind of characters"/","\","'"
example
string id = "some/'id";
You should escape your string , which would lead to :
id = "some\'id";
<script type="text/javascript">
function myFunction(someid) {
someid = someid.replace('#', '\'');
alert(someid);
}
</script>
in Your code
string id = "some'id".Replace("'","#");
this.Controls.Add(new LiteralControl("<input type=\"button\" value=\"Test\" onclick=\"myFunction('" + id + "');\">"));
Hope this will Helps you..

Value Javascript Input

how can i transfer the Value from Input 1 in 2 and add some letters?
<script type="text/javascript">
function doit(){
document.getElementById('input2').value=document.getElementById('input1').value;
}
</script>
Input1: 2342
Input2: pcid2342d
Can some one help me?
Just use the + operator to add a string before and after the input value.
<script type="text/javascript">
function doit(){
document.getElementById('input2').value="pcid" + document.getElementById('input1').value + "d";
}
</script>
String concatenation:
document.getElementById('input2').value = "stuff" + document.getElementById('input1').value + "other stuff";
When dealing with numbers you could start by concatenating with empty string to avoid adding numbers together instead of concatenating to strings (because of operator evaluation order):
document.getElementById('input2').value = "" + 1234 + 567 + document.getElementById('input1').value + 89;
Well you seem to have done the work already, all you need is something to click on to execute it:
<button onclick="doit()">click me</button>
Why don't you try something in jQuery?
$("#Anything").click(function() {
$("#Field1").val($("#Field2").val());
});
The "click" is just a assumption =)

Categories