2
 var file = "{employee}";
 var imgFile = "cancel.json";

  if(file starts with '{' and file ends with '}' ){
     alert("invalid");
  }
  if(imgFile ends with '.json'){
    alert("invalid");
  }
  • How to validate the string starting and ending characters using javascript?
  • In "file" the string should not start with '{' and should not end with '}'
  • In "imgFile " the string should not end with '.json'
  • Does match() works or should i use indexOf()
Rachel
  • 1,131
  • 11
  • 26
  • 43
  • Look at [`charAt()`](http://www.w3schools.com/jsref/jsref_charat.asp), there are some examples there which show how to get the first/last characters of a string – naththedeveloper May 23 '13 at 08:26
  • 1
    See also: [endsWith() in javascript](http://stackoverflow.com/questions/280634/endswith-in-javascript). – Ja͢ck May 23 '13 at 08:31
  • 1
    See also: [Javascript StartsWith](http://stackoverflow.com/questions/646628/javascript-startswith) – Bergi May 23 '13 at 08:42

7 Answers7

7

Does match() works or should i use indexOf()

Neither. Both work, but both search the whole string. It is more efficient to extract the substring in the relevant place and compare it with the one you expect there:

if (file.charAt(0) == '{' && file.charAt(file.length-1) == '}') alert('invalid');
// or:                       file.slice(-1) == '}'
if (imgFile.slice(-5) == '.json') alert('invalid');

Of course, you might as well use a regular expression, with a smart regex engine it should be efficient as well (and your code is more concise):

if (/^\{[\S\s]*}$/.test(file)) alert('invalid');
if (/\.json$/.test(imgFile)) alert('invalid');
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This doesn't strictly satisfy the condition of "must not start with { and must not end with }" – Ja͢ck May 23 '13 at 12:31
  • 1
    @Jack: Right, for that you'd need to replace the `&&` with `||`. I didn't follow the textual description, but modified the OPs code which had that *if starts with and ends with then invalid* … – Bergi May 23 '13 at 13:39
  • Ah! Good point, didn't realise the code and the "specs" were different :) – Ja͢ck May 23 '13 at 14:12
4
if (str.charAt(0) == 'a' && str.charAt(str.length-1) == 'b') {
    //The str starts with a and ends with b
}

Untested, but it should work

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
CaptainCarl
  • 3,411
  • 6
  • 38
  • 71
1

This

 /^\{.*\}$/.test (str)

will return true of str starts with { and ends in }

HBP
  • 15,685
  • 6
  • 28
  • 34
0

you can use the javascript startswith() and endswith()

    function strStartsWith(str, prefix) {
    return str.indexOf(prefix) === 0;
}
function strEndsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
or
function strEndsWith(str, suffix) {
var re=new RegExp("."+suffix+"$","i");
if(re.test(str)) alert("invalid file");

}

or you can also use it like this:

String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
 and

String.prototype.endsWith = function (str){
return this.slice(-str.length) == str;
};
argentum47
  • 2,385
  • 1
  • 18
  • 20
0

In "file" the string should not start with '{' and should not end with '}'

    if (file.charAt(0) == '{' || file.charAt(file.length - 1) == '}') {
        throw 'invalid file';
    }

In "imgFile " the string should not end with '.json'

    if (imgFile.lastIndexOf('.json') === imgFile.length - 5) {
        throw 'invalid imgFile';
    }
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0
file.indexOf("{") == 0 && file.lastIndexOf("}") == file.length-1
CBroe
  • 91,630
  • 14
  • 92
  • 150
0

You have many options when it comes to string verification, you can use regex..different string methods can also do the job for you...

But your problem you can try the following:

if(file.indexOf('{') == 0 && file.indexOf('}') == file.length - 1) 
        alert('invalid');

For the second part, its most likely that you are looking for extension for the file so you can use the following:

if(imgFile.split('.').pop() == "json")
    alert('invalid');   

Hope this helps....

Hmxa Mughal
  • 394
  • 1
  • 4
  • 17