Home > Software engineering >  how to check if the input 42 to length long and must have 0x at the beginning
how to check if the input 42 to length long and must have 0x at the beginning

Time:08-12

I need the input to be 42 to length long and must have 0x at the beginning like 0xcollaboratingcorerivnefvting so how can i achieve that using jquery

<html lang="en-US"  dir="ltr">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <link rel="stylesheet" href="/css/home.css" />
    <script type="text/javascript" src="//code.jquery.com/jquery-compat-git.js"></script>
  </head>

  <body>
    <input type="text"   id="check" data-minlength="42" name="address" onKeyDown="javascript: var keycode = keyPressed(event); if(keycode==32){ return false; }" />

    <button  type="button" id="kato" disabled>Continue</button>

    <script type="text/javascript">
      //<![CDATA[
      $(function () {
        $("#kato").prop("disabled", true);
      
        $("#check").on("input", function (e) {
          if (this.value.length === 42) {
            $("#kato").prop("disabled", false);
           
          } 

          else {
            $("#kato").prop("disabled", true);
            
          }
        });
      });
      //]]>
    </script>
  </body>
</html>

CodePudding user response:

If you want validate a string with 0x then use regex= /^0x[0-9a-f] /gi (i: for ignoring case for hexadecimal code). Validate with regex.test(inputString)

If you want to check the length of hexadecimal code exactly 42 lengths (including the length of 0x): then use regex= /^0x[0-9a-f]{40}$/gi.

/^0x[0-9a-f]{40}$/gi.test('0x1234567891113151719212325272931333537a42') // true
/^0x[0-9a-f]{40}$/gi.test('0x1234567891113151719212325272931333537a42a') // false
/^0x[0-9a-f]{40}$/gi.test('0x1234567891113151719212325272931333537$42') // false
/^0x[0-9a-f]{40}$/gi.test('0x1234567891113151719212325272931333537a4')// false

Here you may test the string start with 0x along with length.

CodePudding user response:

Use regular expressions. You can find useful info about them here. Ready code is below. If I didn't get your question right, feel free to point at that. All the best!

<html lang="en-US"  dir="ltr">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width,initial-scale=1" />
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    </head>
    <body>
        <input type="text" id="check" data-minlength="42" name="address" onKeyDown="onInputKeyDown" />
        <button  type="button" id="kato" disabled>Continue</button>
        <script type="text/javascript">
            function onInputKeyDown() {
                var keycode = keyPressed(event);
                if(keycode == 32) {
                    return false;
                }
            }
            //<![CDATA[
            $(function () {
                $("#kato").prop("disabled", true);
                $("#check").on("input", function (e) {
                    var regexpr = /^0x[a-z0-9]*$/;
                    if (regexpr.test(this.value) && this.value.length === 42) {
                        $("#kato").prop("disabled", false);
                    } else {
                        $("#kato").prop("disabled", true);
                    }
                });
            });
        //]]>
        </script>
    </body>
</html>

  • Related