How to make Joi regex() validation fail if the string contains “ ” (whitespace)?












0















I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)



I tried the following schema, but Joi does let it go through:



  const schema = { 
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}


So, if I submit "JOI 777" the string is considered valid.



What am I doing wrong?
Thanks in advance,










share|improve this question























  • Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace /^S+$/ , IOW: if a whitepace appears it should fail.

    – Keith
    Nov 20 '18 at 12:03


















0















I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)



I tried the following schema, but Joi does let it go through:



  const schema = { 
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}


So, if I submit "JOI 777" the string is considered valid.



What am I doing wrong?
Thanks in advance,










share|improve this question























  • Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace /^S+$/ , IOW: if a whitepace appears it should fail.

    – Keith
    Nov 20 '18 at 12:03
















0












0








0








I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)



I tried the following schema, but Joi does let it go through:



  const schema = { 
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}


So, if I submit "JOI 777" the string is considered valid.



What am I doing wrong?
Thanks in advance,










share|improve this question














I have the a vehicle registration number being validated by Joi in Node.js and need it to reject any string that contains whitespace (space, tab, etc.)



I tried the following schema, but Joi does let it go through:



  const schema = { 
regNo: Joi.string()
.regex(/^.*S*.*$/)
.required()
.trim()
}


So, if I submit "JOI 777" the string is considered valid.



What am I doing wrong?
Thanks in advance,







javascript node.js joi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 11:47









Nikolay ChekanNikolay Chekan

31




31













  • Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace /^S+$/ , IOW: if a whitepace appears it should fail.

    – Keith
    Nov 20 '18 at 12:03





















  • Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace /^S+$/ , IOW: if a whitepace appears it should fail.

    – Keith
    Nov 20 '18 at 12:03



















Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace /^S+$/ , IOW: if a whitepace appears it should fail.

– Keith
Nov 20 '18 at 12:03







Your first part of you regex is going to match anything,. I think what you want is a lot simpler, match any none-whitespace /^S+$/ , IOW: if a whitepace appears it should fail.

– Keith
Nov 20 '18 at 12:03














2 Answers
2






active

oldest

votes


















1














This part of your regex -> /^.* is saying match anything, so the rest of your regEx is pretty much short circuited.



So your RegEx is a bit simpler, /^S+$/



This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()..



eg.






const tests = [
"JOI 777", //space in the middle
"JOI777", //looks good to me
" JOI777", //space at start
"JOI777 ", //space at end
"JOtI77", //tab
"ABC123", //another one that seems ok.
"XYZn111" //newline
];

tests.forEach(t => {
console.log(`${!!t.match(/^S+$/)} "${t}"`);
});








share|improve this answer































    0














    for skipping whitespace from string just use:



    "hello world".replace(/s/g, "");


    if you have more than One space use this :



    "this string has more than one space".replace(/ /g, '');


    for more detail see this link below:
    Remove whitespaces inside a string in javascript






    share|improve this answer



















    • 1





      if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

      – Keith
      Nov 20 '18 at 12:21













    • tnx for the tips I realized now

      – babak abadkheir
      Nov 20 '18 at 12:25











    • This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

      – unflores
      Nov 20 '18 at 13:30











    • Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

      – Nikolay Chekan
      Nov 21 '18 at 9:27











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392351%2fhow-to-make-joi-regex-validation-fail-if-the-string-contains-whitespace%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This part of your regex -> /^.* is saying match anything, so the rest of your regEx is pretty much short circuited.



    So your RegEx is a bit simpler, /^S+$/



    This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()..



    eg.






    const tests = [
    "JOI 777", //space in the middle
    "JOI777", //looks good to me
    " JOI777", //space at start
    "JOI777 ", //space at end
    "JOtI77", //tab
    "ABC123", //another one that seems ok.
    "XYZn111" //newline
    ];

    tests.forEach(t => {
    console.log(`${!!t.match(/^S+$/)} "${t}"`);
    });








    share|improve this answer




























      1














      This part of your regex -> /^.* is saying match anything, so the rest of your regEx is pretty much short circuited.



      So your RegEx is a bit simpler, /^S+$/



      This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()..



      eg.






      const tests = [
      "JOI 777", //space in the middle
      "JOI777", //looks good to me
      " JOI777", //space at start
      "JOI777 ", //space at end
      "JOtI77", //tab
      "ABC123", //another one that seems ok.
      "XYZn111" //newline
      ];

      tests.forEach(t => {
      console.log(`${!!t.match(/^S+$/)} "${t}"`);
      });








      share|improve this answer


























        1












        1








        1







        This part of your regex -> /^.* is saying match anything, so the rest of your regEx is pretty much short circuited.



        So your RegEx is a bit simpler, /^S+$/



        This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()..



        eg.






        const tests = [
        "JOI 777", //space in the middle
        "JOI777", //looks good to me
        " JOI777", //space at start
        "JOI777 ", //space at end
        "JOtI77", //tab
        "ABC123", //another one that seems ok.
        "XYZn111" //newline
        ];

        tests.forEach(t => {
        console.log(`${!!t.match(/^S+$/)} "${t}"`);
        });








        share|improve this answer













        This part of your regex -> /^.* is saying match anything, so the rest of your regEx is pretty much short circuited.



        So your RegEx is a bit simpler, /^S+$/



        This is then saying, from the start to the end, everything has to be a None whitespace.. Also seen as this checks everything for whitespace, you could also take out your .trim()..



        eg.






        const tests = [
        "JOI 777", //space in the middle
        "JOI777", //looks good to me
        " JOI777", //space at start
        "JOI777 ", //space at end
        "JOtI77", //tab
        "ABC123", //another one that seems ok.
        "XYZn111" //newline
        ];

        tests.forEach(t => {
        console.log(`${!!t.match(/^S+$/)} "${t}"`);
        });








        const tests = [
        "JOI 777", //space in the middle
        "JOI777", //looks good to me
        " JOI777", //space at start
        "JOI777 ", //space at end
        "JOtI77", //tab
        "ABC123", //another one that seems ok.
        "XYZn111" //newline
        ];

        tests.forEach(t => {
        console.log(`${!!t.match(/^S+$/)} "${t}"`);
        });





        const tests = [
        "JOI 777", //space in the middle
        "JOI777", //looks good to me
        " JOI777", //space at start
        "JOI777 ", //space at end
        "JOtI77", //tab
        "ABC123", //another one that seems ok.
        "XYZn111" //newline
        ];

        tests.forEach(t => {
        console.log(`${!!t.match(/^S+$/)} "${t}"`);
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 12:13









        KeithKeith

        8,8301820




        8,8301820

























            0














            for skipping whitespace from string just use:



            "hello world".replace(/s/g, "");


            if you have more than One space use this :



            "this string has more than one space".replace(/ /g, '');


            for more detail see this link below:
            Remove whitespaces inside a string in javascript






            share|improve this answer



















            • 1





              if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

              – Keith
              Nov 20 '18 at 12:21













            • tnx for the tips I realized now

              – babak abadkheir
              Nov 20 '18 at 12:25











            • This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

              – unflores
              Nov 20 '18 at 13:30











            • Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

              – Nikolay Chekan
              Nov 21 '18 at 9:27
















            0














            for skipping whitespace from string just use:



            "hello world".replace(/s/g, "");


            if you have more than One space use this :



            "this string has more than one space".replace(/ /g, '');


            for more detail see this link below:
            Remove whitespaces inside a string in javascript






            share|improve this answer



















            • 1





              if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

              – Keith
              Nov 20 '18 at 12:21













            • tnx for the tips I realized now

              – babak abadkheir
              Nov 20 '18 at 12:25











            • This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

              – unflores
              Nov 20 '18 at 13:30











            • Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

              – Nikolay Chekan
              Nov 21 '18 at 9:27














            0












            0








            0







            for skipping whitespace from string just use:



            "hello world".replace(/s/g, "");


            if you have more than One space use this :



            "this string has more than one space".replace(/ /g, '');


            for more detail see this link below:
            Remove whitespaces inside a string in javascript






            share|improve this answer













            for skipping whitespace from string just use:



            "hello world".replace(/s/g, "");


            if you have more than One space use this :



            "this string has more than one space".replace(/ /g, '');


            for more detail see this link below:
            Remove whitespaces inside a string in javascript







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 12:11









            babak abadkheirbabak abadkheir

            479311




            479311








            • 1





              if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

              – Keith
              Nov 20 '18 at 12:21













            • tnx for the tips I realized now

              – babak abadkheir
              Nov 20 '18 at 12:25











            • This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

              – unflores
              Nov 20 '18 at 13:30











            • Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

              – Nikolay Chekan
              Nov 21 '18 at 9:27














            • 1





              if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

              – Keith
              Nov 20 '18 at 12:21













            • tnx for the tips I realized now

              – babak abadkheir
              Nov 20 '18 at 12:25











            • This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

              – unflores
              Nov 20 '18 at 13:30











            • Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

              – Nikolay Chekan
              Nov 21 '18 at 9:27








            1




            1





            if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

            – Keith
            Nov 20 '18 at 12:21







            if you have more than One space use this, your first one will remove multiple spaces too,.. Your second one will ONLY remove spaces, not tabs / newline etc.

            – Keith
            Nov 20 '18 at 12:21















            tnx for the tips I realized now

            – babak abadkheir
            Nov 20 '18 at 12:25





            tnx for the tips I realized now

            – babak abadkheir
            Nov 20 '18 at 12:25













            This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

            – unflores
            Nov 20 '18 at 13:30





            This doesnt really answer the person's question. They are looking to have joi reject a value not modify incomming values.

            – unflores
            Nov 20 '18 at 13:30













            Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

            – Nikolay Chekan
            Nov 21 '18 at 9:27





            Hi all, thanks for the answers. Yes, the answer provided by Keith worked as needed within Joi. Much appreciate everyone's attention!

            – Nikolay Chekan
            Nov 21 '18 at 9:27


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392351%2fhow-to-make-joi-regex-validation-fail-if-the-string-contains-whitespace%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)