If String starts with a number check the length of the string - javascript

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?

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.

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

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

Related

Why is my JS function always returning true? [duplicate]

This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.

=== won't work even if the statement is true

var age =prompt("What is your age?");
if (age === 21) {
console.log("Happy 21st Birthday!");
}
When I write 21 in the prompt, it gives me an undefined, if I replace the === with == then it will work. Why? 21 is the same type and value as the 21 I write in the prompt
Your variable age gets a String from the prompt.
For it to work you need to convert it to an int with the operator +:
If the use of the + operator feels strange to you in this case, you can always use the function parseInt() instead. It will achieve the same result.
var age = +prompt("What is your age?");
// ^ Converts your String to an int
if (age === 21) {
console.log("Happy 21st Birthday!");
}
The prompt() function returns a string. You check for an integer.
Basically == checks if the value of the variable is equal.
=== checks if the value and type is equal.
You can find more about it here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
=== is check the value and type of variable and prompt return string value thats why === return false because prompt return string 21 and you compare with int 21 so return false
so below to way to get your output using == or string to int conversion
var age = prompt("What is your age?");
if (age == 21) {
console.log("Happy 21st Birthday!");
}
//OR
if (parseInt(age) === 21) {
console.log("Happy 21st Birthday!");
}

Why doesn't my equality comparison using = (a single equals) work correctly? [duplicate]

This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.

How to check string length in JavaScript?

How can I check the length of in String in JavaScript? Here is a small code example:
if(value != null && value != "" && value.length !== 10 && !value.match(/^\d*$/)){
// do something
}
The expression 'value.length !== 10' doesn´t work. A String must contain at least 10 characters. How can I solve this problem?
Instead of match, test can be used with proper regex \d{10,}.
if (value && /^\d{10,}$/.test(value.trim()))
To Get the string length of a value for example:
var value="This is a string";
var string_length=value.length;
/*The string length will result to 16*/
Hope this helps
var regex = new RegExp(/^\d*$/),
value = $.trim("1234567890");
if (value.length >= 10 && regex.exec(value)) {
// correct
} else {
// incorrect
}

HTML textfield whose values cannot be 0 using Javascript

I was trying to make a javascript function which will check if the user entered value inside a text field cannot be less than 9 digits & it cannot be all 0s.
This is what I made
function CheckField(field)
{
if (field.value.length <9 || field.value=="000000000")
{
alert("fail");
field.focus();
return false;
}
else
{
return true;
}
}
<input type ="text" id="number1" onBlur='return CheckField(this)'>
But this doesnt check the condition where user enters more than 9 values and all 0's. It checks only for 1 condition that is with exact 9 zeros 000000000
So, if I understand that right you want the user to be able to enter a number with more than 9 digits, but they cannot be all zeros, right?
This can be done with a regexp:
var value; // Obtain it somehow
if (/^\d{9,}$/.test(value) && !/^0+$/.test(value)) {
// ok
}
What this checks is whether the value is at lest 9 digits (it does not allow anything but digits) and that they are not all 0s.
This should check for both conditions:
function CheckField(field){
return !/0{9}/.test(field.value) && /\d{9}/.test(field.value);
}
Try something like this:
var valueEntered = field.value;
if (parseInt(valueEntered) == 0) ...
or if you wanted to check if it was a number as well:
if (!(parseInt(valueEntered) > 0))
Two options spring to mind. You can try parsing the value as a number and test for isNaN or != 0
var parsed = parseInt(field.value, 10);
if(field.value.length < 9 || !(isNaN(parsed) || parsed != 0)){
alert("fail");
... rest of code
}
Or you could use a regex
if(field.value.length < 9 || !/[^0]/.test(field.value){
alert("fail");
... rest of code
}
The first option is probably quicker.
try this:
if (field.value.length <9 || field.value.replace("0","") == "")

Categories