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()
3
  • Look at charAt(), there are some examples there which show how to get the first/last characters of a string Commented May 23, 2013 at 8:26
  • 1
    See also: endsWith() in javascript. Commented May 23, 2013 at 8:31
  • 1
    See also: Javascript StartsWith Commented May 23, 2013 at 8:42

7 Answers 7

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');
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't strictly satisfy the condition of "must not start with { and must not end with }"
@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
Ah! Good point, didn't realise the code and the "specs" were different :)
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

Comments

1

This

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

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

1 Comment

…and does not contain a line terminator :-)
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';
    }

Comments

0
file.indexOf("{") == 0 && file.lastIndexOf("}") == file.length-1

Comments

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....

Comments

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;
};

3 Comments

Works OK unless suffix contains special RegEx chars like . or / or *.
@HBP: Which even is the case here (.json), so this answer is invalid
you can just ask to correct it, if you the power to downvote and >1k rep doesnot mean you can go on downvoting..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.