I have a Javascript function like this:
function validateSessionName() {
var session_name = document.getElementById('session_name').value;
var submitButton = document.getElementById('submit');
document.getElementById('session_error').innerHTML='';
submitButton.removeAttribute('disabled');
var filter = /^[A-Za-z0-9]+$/;
if (filter.test(session_name)){
return true;
} else {
document.getElementById('session_error').innerHTML="Invalid Session Name!";
submitButton.setAttribute('disabled', 'disabled');
}
}
This validates that only numbers and alphanumerical characters are entered into the form. However, there might be a case when a session name can consist of !#$#$#(%*. All these characters are available on the keyboard. I don't want my application to accept these weird characters: 工具, which make my application crash. How can I do that? Thanks.
It depends what you mean by "English keyboard" and "weird characters".
You're probably thinking of limiting your input to ASCII characters, but even then you can get "weird" characters such as null, and a host of control characters. Perhaps limiting the characters to the ASCII range [32, 126] is what you're after? (Those are all printable characters that can be input on a US keyboard.)
/^[\x20-\x7E]+$/
The characters that match that regex are (note the leading space):
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? # A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
https://blogs.oracle.com/shankar/entry/how_to_handle_utf_8
How to remove invalid UTF-8 characters from a JavaScript string?
Or just google something to this like
Related
I have a large text from which I read data according to the scheme. Key words are placed in the "smallArtName" array. The scheme looks like this:
(key word) xxx (cordX|cordY)
I can't convert the string I received to a number. It seems to me that the reason is white space, visible in the terminal in the picture. I tried to use the replace method which works for sample text, but not for my value.
I'm a beginner and I could probably do it simpler, but the code I wrote works, and this is the most important thing for now.
for (i = 0; i < smallArtName.length; i++) {
var n = art.artPrintScreen.indexOf(smallArtName[i]);
if (n > -1) {
var tempString = art.artPrintScreen.substring(n, n + 100);
betweenChar = tempString.indexOf('|');
for (k = betweenChar - 10; k <= betweenChar + 10; k++) {
if (tempString[k] == '(') {
xStart = k;
}
if (tempString[k] == ')') {
yEnd = k;
}
}
cordX = tempString.slice(xStart + 1, betweenChar);
cordY = tempString.slice(betweenChar + 1, yEnd);
strTest = " t est".replace(/\s/g, '')
var cordY2 = cordY.replace(/\s/g, '')
console.log(typeof (cordY))
console.log(cordY2)
console.log(cordY2[0])
console.log(cordY2[1])
console.log(cordY2[2])
console.log(cordY2[3])
console.log(cordY2[4])
console.log(cordY2[5])
console.log(strTest)
var cordYtest = parseInt(cordY2, 10);
console.log(cordYtest)
}
}
Terminal:
-181
-
1
8
1
test
NaN
string
-154
-
1
5
4
test
NaN
string
104
1
0
4
undefined
test
NaN
Fragment of input text:
Ukryta twierdza (Mapa podziemi I) 153 (−72|−155)
Ukryta twierdza (Amfora Mgły VI) 135 (73|104)
Ukryta twierdza (Mapa podziemi IV) 131 (154|−72)
Analysing your sample input strings, I found some unicode characters \u202c and \u202d that should be stripped before converting to number. Also, the negative values are prefixed by the character −, which is different than minus -, se we need to replace it. That being said, all parsing could be done with a single regex:
var input = "Ukryta twierdza (Mapa podziemi I) 153 (−72|−155)";
input = input.replace(/\u202d|\u202c/g, "");
input = input.replace(/−/g, "-");
var m = input.match(/.*\((.*)\)\s*(.+?)\s*\((.+)\|(.+)\)/);
console.log(m);
console.log(parseInt(m[3]));
console.log(parseInt(m[4]));
Explaining the regex:
.* - Something that will be ignored
\((.*)\) - Something enclosed in parenthesis
\s*(.+?)\s* - Something possibly surrounded by spaces
\((.+)\|(.+)\) - Two parts split by a | and enclosed by parenthesis
I stuck on this can you help in JavaScript Alien message
Allowed languages
JavaScript
Your task is to translate a message in some alien language (let's call it Alienski).
The message could be created by following simple rules and from two known languages, English and Spanish.
Each word in Alienski is constructed by subtracting the letters from English and Spanish (absolute value) and that is the resulting letter.
There are two special cases. If in each of the words the symbol is '-' (hyphen) or ' ' (space) it is mandatory for it to be kept this way.
There won't be a case with a '-' (hyphen) and a ' ' (space) at the same time.
If one of the words is with more letters than the other just add the letters from the longer word to the result.
Example:
Copy
talk
hablar
Copy
a b c d....
0 1 2 3....
t - h = | 19 - 7 | = 12 = m
a - a = | 0 - 0 | = 0 = a
l - b = | 11 - 1 | = 10 = k
k - l = | 10 - 11 | = 1 = b
empty - a = a
empty - r = r
Result:
makbar
I stuck from 3 hours on this. Here is my code so far
let englishWord = 'talk'
let spanishWord = 'hablar'
let engToDigit = [];
let spnToDigit = [];
let alien = [];
for (var i = 0; i < englishWord.length; i++) {
engToDigit.push(englishWord.charCodeAt(i))
}
for (var y = 0; y < spanishWord.length; y++) {
spnToDigit.push(spanishWord.charCodeAt(y))
}
let result = engToDigit.map((a, i) => a - spnToDigit[i]);
for (let index = 0; index < result.length; index++) {
result[index] += 97;
console.log(result);
What it sounds like you need is to take this in small steps. First I would make a function that iterates through a string and converts each letter to its ASCII code. Try the following order:
Check if code is uppercase then get the numeric value.
Make sure charCode is greather than 96 and charCode is less than 123
Then turn all the codes to their numeric value by running and
collecting in an array: charCode - 97
Else check if the code is lower case then get the numeric value.
Make sure that charCode is greater than 64 and charCode is less than 91.
Then turn all the codes to their numeric value by running and collecting in an array: charCode - 65
Else just add the value to the array.
Outside the above loop return an array that is joined.
When the array is joined it will be a string like "19,0,11,10,-,7,0,1,11,0,17".
Check if there is a space or a hyphen.
Then you can split the array on the result of step 9.
Then split each array on ",".
Loop through each array and subtract the values.
Convert the values back by adding 65 - because there is no way at this point to know if a character was upper case.
Then use String.fromCharCode(##) to convert the code back to the non-readable alien word.
While working on a multilingual site (with Japanese and Chinese languages ) where users were allowed to enter characters in regional language. I had this requirement to validate user inputs based on memory taken by each character as character can be
single byte, double byte or triple byte.
I used following solution for this as mentioned in the answer.
Characters can be single byte ,double byte, triple byte and so on. Single byte follows in a particular range. Same thing is true for other characters. Based on this I have created following functions that will calculate the size of a string on the basis of memory
function getByteLength(normal_val) {
// Force string type
normal_val = String(normal_val);
var byteLen = 0;
for (var i = 0; i < normal_val.length; i++) {
var c = normal_val.charCodeAt(i);
byteLen += c < (1 << 7) ? 1 :
c < (1 << 11) ? 2 :
c < (1 << 16) ? 3 :
c < (1 << 21) ? 4 :
c < (1 << 26) ? 5 :
c < (1 << 31) ? 6 : Number.NaN;
}
return byteLen;
}
So above function can be modified to find out whether a function is single byte or multi-bytes.
Following js fiddle determines the size of entered text in terms of memory.
http://jsfiddle.net/paraselixir/d83oaa3v/5/
so if string has x characters and memory size is y
so if x === y then all characters are single bytes
if 2*x === y then all characters are double bytes
otherwise string is combination of single and double/multi bytes.
When changing to TypeScript I'm not allowed to use escape(string) anymore because it's deprecated. The reason I still use it is that the alternatives encodeURI and encodeURIComponent give a different results.
var s = "Å"
console.log(escape(s));
console.log(encodeURI(s));
console.log(encodeURIComponent(s));
I don't use this for URLs, but for a CSV export.
What are other alternatives that will give me the same result as escape(string)?
In EcmaScript spec there is algorithm:
Call ToString(string).
Compute the number of characters in Result(1).
Let R be the empty string.
Let k be 0.
If k equals Result(2), return R.
Get the character at position k within Result(1).
If Result(6) is one of the 69 nonblank ASCII characters ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz 0123456789 #*_+-./, go to step 14.
Compute the 16-bit unsigned integer that is the Unicode character encoding of Result(6).
If Result(8), is less than 256, go to step 12.
Let S be a string containing six characters “%uwxyz” where wxyz are four hexadecimal digits encoding the
value of Result(8).
Go to step 15.
Let S be a string containing three characters “%xy” where xy are two hexadecimal digits encoding the
value of Result(8).
Go to step 15.
Let S be a string containing the single character Result(6).
Let R be a new string value computed by concatenating the previous value of R and S.
Increase k by 1.
Go to step 5.
which can be coded like this:
(function(global) {
var allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#*_+-./,';
global.escapeString = function(str) {
str = str.toString();
var len = str.length, R = '', k = 0, S, chr, ord;
while(k < len) {
chr = str[k];
if (allowed.indexOf(chr) != -1) {
S = chr;
} else {
ord = str.charCodeAt(k);
if (ord < 256) {
S = '%' + ("00" + ord.toString(16)).toUpperCase().slice(-2);
} else {
S = '%u' + ("0000" + ord.toString(16)).toUpperCase().slice(-4);
}
}
R += S;
k++;
}
return R;
};
})(typeof window == 'undefined' ? global : window);
I'm using angularjs for my application and found this issue on google chrome, the problem is I have this URL:
http://127.0.0.1/#/fa/episode/1315019/برنامه-خوب
but when I click on this links it shows:
http://127.0.0.1/#/fa/episode/1315019/%D9%82%D8%B3%D9%85%D8%AA-%D8%A2%D8%AE%D8%B1
I'm using $routeProvider for my routing, it works fine in firefox but I have issue with that in google chrome.
why is that and how should I resolve this issue?
You cant fix this "problem" but it actually should not be a problem at all.
URLs can only contain these characters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~
! * ' ( ) ; : # & = + $ , / ? % # [ ]
All other characters, like <, § and the characters contained in برنامه-خوب will get encoded.
Encoding means, that they get replaced by a combination of valid characters, in your case برنامه-خوب gets replaced by %D9%82%D8%B3%D9%85%D8%AA-%D8%A2%D8%AE%D8%B1 which is no issue, its the way how an URL works, you can not change it.