JavaScript String Syntax Checking


Joined
Jun 29, 2022
Messages
2
Reaction score
0
I am trying to write an Function which should check a String.
It should be True if the String contains a number between 101 and 132.
And it should be false in case of the number is smaller or bigger
Now i am struggling to check if there is just the number in it Nothing else.(Like G110-> that should be false)
Please help, first time i am using JavaScript and hopefully last ;-)
 
Ad

Advertisements

Joined
Nov 13, 2020
Messages
151
Reaction score
24
The function isNaN(your string here) will return TRUE if the string is not a number.
 
Joined
Nov 13, 2020
Messages
151
Reaction score
24
You should report your plc as the only one in the world where isNaN doesn't work.
 
Ad

Advertisements

Joined
Jul 3, 2022
Messages
11
Reaction score
0
Try this:

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style>
    body{
    text-align: center;
    }
    div{
    padding: 20px;
    }
    input{
    text-align: center;
    }
    button{
    cursor: pointer;
    }
    </style>
  
  </head>
  <body>
    <div>
      <input type="text" id="inp" size="50" placeholder="String to parse"    />
    </div>
    <div>
      <button>Check it</button>
    </div>
    <div>
      <input type="text" id="res" placeholder="Result" />
    </div>
    <script>
    /*
    I am trying to write an Function which should check a String.
It should be True if the String contains a number between 101 and 132.
And it should be false in case of the number is smaller or bigger
Now i am struggling to check if there is just the number in it Nothing else.(Like G110-> that should be false)
Please help, first time i am using JavaScript and hopefully last ;-)

https://www.thecodingforums.com/threads/javascript-string-syntax-checking.973830/
    */
   
    document.querySelector('button').addEventListener('click', function(){
    const val = Number(document.querySelector('#inp').value.replace(/\D+/ig, ''));
    if(!val) return;
   document.querySelector('#res').value = 101 < val && val < 132 ? true : false;
    });
    </script>
  </body>
</html>
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top