lundi 30 octobre 2017

How to read the first n character of a string without reading the entire sting in javascripts?

I am testing and rewriting my input validation functions. I though that maybe some users somehow provide huge strings and cause problems (?), so I want my functions to read only the first n characters of the string. I do not want to read the entire string.

If I get the first n characters in this way:

var couldbehuge = $('#inputbox')
var onlyapart = couldbehuge.slice(0:20)

It looks like that I am temporary storing a user input. Then, if I do this:

var onlyapart = $('#inputbox').slice(0:20)

Is is safer because I am not storing the user input in a variable?

Also, I can read the string character by character:

var i, letter;
var onlyapart ="";
for(i=0; i<20; i++){
  letter = $('#inputbox')[i]
  onlyapart += letter
}

Any better idea?

Aucun commentaire:

Enregistrer un commentaire