1

i want to use javascript in a .pdf file. I want to check if a string starts with "1" or with a letter.

If the string starts with "1" i want to check the length of the string. If the string is 18 chars long, then i want to call my own created function. If the String is shorter than 18 chars i want to display a message.

If the string starts with a letter, i want to check the length of the string. If the string is 11 chars long, then i want to call my own created function. If the String is shorter than 11 chars i want to display a message.

But how i can do this?

renokl2014
  • 121
  • 1
  • 11
  • Check [this answer](http://stackoverflow.com/questions/12066118/reading-pdf-file-using-javascript) to your first problem. Good luck! – lsborg Dec 18 '14 at 12:02
  • Do you know how to read pdf and process it with javascript. because string length validation is simple , but i dont think that you only need that. – bharatpatel Dec 18 '14 at 13:04

3 Answers3

0

You can use something similar to this:

if (typeof variableName == 'string' || variableName instanceof String){
   if(variableName[0] == '1'){
     if(variableName.length == 18){
       //call your method
       console.log("It's 18th character long");
     } else if(variableName.length == 11){
       //call another method
       console.log("It's 11th character long");
     }
   }
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

You can select the first character like so - string[0]. Strings behave like arrays in this way. You can test the length of a string like so - string.length

var string1 = "1dgfe";

if (string1[0] == 1 && string1.length > 18){
    yourfunction();
} else if (string1[0] == 1 && string1.length < 18){
    console.log('your message');
}
JasTonAChair
  • 1,948
  • 1
  • 19
  • 31
0
var string = "Your String";
if(string[0] === '1'){
    if(string.length >= 18 )
        callYourFunction();
    else
        alert("Your Message");
}
else if(isNaN(string[0])){
    if(string.length >= 11 )
        callYourFunction();
    else
        alert("Your Message");
}

Here, string.length returns the length of the string as integer. isNaN() checks whether the parameter is not a number. It returns false if the parameter is a number.

amulya349
  • 1,210
  • 2
  • 17
  • 26