Angular Form Input block (space) REGEX












1















I have an input field in my Angular component in which i want to not allow a user to be able to type a (space).



I've tried using



<input type="text" [(ngModel)]="inputText" pattern="[a-zA-Z]">


which wasn't what i wanted, and it didn't work anyways!



Does anybody know what the correct regex pattern to just block the (space) key is? And what is the correct way to use the pattern, as the above pattern didn't work...



Thanks in advance.










share|improve this question























  • Use a negative set: [^ ] or if you want to disallow every whitespace character: [^s]

    – ssc-hrep3
    Nov 17 '18 at 19:45













  • Did any of these answers fix the issue for you?

    – SiddAjmera
    Nov 18 '18 at 17:16
















1















I have an input field in my Angular component in which i want to not allow a user to be able to type a (space).



I've tried using



<input type="text" [(ngModel)]="inputText" pattern="[a-zA-Z]">


which wasn't what i wanted, and it didn't work anyways!



Does anybody know what the correct regex pattern to just block the (space) key is? And what is the correct way to use the pattern, as the above pattern didn't work...



Thanks in advance.










share|improve this question























  • Use a negative set: [^ ] or if you want to disallow every whitespace character: [^s]

    – ssc-hrep3
    Nov 17 '18 at 19:45













  • Did any of these answers fix the issue for you?

    – SiddAjmera
    Nov 18 '18 at 17:16














1












1








1








I have an input field in my Angular component in which i want to not allow a user to be able to type a (space).



I've tried using



<input type="text" [(ngModel)]="inputText" pattern="[a-zA-Z]">


which wasn't what i wanted, and it didn't work anyways!



Does anybody know what the correct regex pattern to just block the (space) key is? And what is the correct way to use the pattern, as the above pattern didn't work...



Thanks in advance.










share|improve this question














I have an input field in my Angular component in which i want to not allow a user to be able to type a (space).



I've tried using



<input type="text" [(ngModel)]="inputText" pattern="[a-zA-Z]">


which wasn't what i wanted, and it didn't work anyways!



Does anybody know what the correct regex pattern to just block the (space) key is? And what is the correct way to use the pattern, as the above pattern didn't work...



Thanks in advance.







regex angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 19:42









Tom O'BrienTom O'Brien

50021646




50021646













  • Use a negative set: [^ ] or if you want to disallow every whitespace character: [^s]

    – ssc-hrep3
    Nov 17 '18 at 19:45













  • Did any of these answers fix the issue for you?

    – SiddAjmera
    Nov 18 '18 at 17:16



















  • Use a negative set: [^ ] or if you want to disallow every whitespace character: [^s]

    – ssc-hrep3
    Nov 17 '18 at 19:45













  • Did any of these answers fix the issue for you?

    – SiddAjmera
    Nov 18 '18 at 17:16

















Use a negative set: [^ ] or if you want to disallow every whitespace character: [^s]

– ssc-hrep3
Nov 17 '18 at 19:45







Use a negative set: [^ ] or if you want to disallow every whitespace character: [^s]

– ssc-hrep3
Nov 17 '18 at 19:45















Did any of these answers fix the issue for you?

– SiddAjmera
Nov 18 '18 at 17:16





Did any of these answers fix the issue for you?

– SiddAjmera
Nov 18 '18 at 17:16












3 Answers
3






active

oldest

votes


















1














Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.



If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:






<input type="text" (keydown.space)="$event.preventDefault()">





Here's also a Sample StackBlitz for your ref.






share|improve this answer































    0














    You can use the following pattern:



    <input pattern="[^s]*">




    • [^s] is a negative set which matches every character which is not in the set.


    • s matches a white space character (e.g. space, tab, etc.)


    • * matches 0 or more character of the preceding item




    Here is an example of how the browser checks if the pattern is correct (i.e. Google Chrome for example does not allow you to submit the form if there is a whitespace character in it. Test it here (enter a string containing a white space and hit Submit):






    <form>
    <input pattern="[^s]*">
    <button type="submit">Submit</button>
    </form>








    share|improve this answer

































      0














      The best way of addressing this problem is by writing the directive which you can use on multiple locations.



      Here is the Stackblitz sample for the same






      share|improve this answer

























        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%2f53354888%2fangular-form-input-block-space-regex%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.



        If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:






        <input type="text" (keydown.space)="$event.preventDefault()">





        Here's also a Sample StackBlitz for your ref.






        share|improve this answer




























          1














          Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.



          If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:






          <input type="text" (keydown.space)="$event.preventDefault()">





          Here's also a Sample StackBlitz for your ref.






          share|improve this answer


























            1












            1








            1







            Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.



            If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:






            <input type="text" (keydown.space)="$event.preventDefault()">





            Here's also a Sample StackBlitz for your ref.






            share|improve this answer













            Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.



            If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:






            <input type="text" (keydown.space)="$event.preventDefault()">





            Here's also a Sample StackBlitz for your ref.






            <input type="text" (keydown.space)="$event.preventDefault()">





            <input type="text" (keydown.space)="$event.preventDefault()">






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '18 at 20:41









            SiddAjmeraSiddAjmera

            13.5k31137




            13.5k31137

























                0














                You can use the following pattern:



                <input pattern="[^s]*">




                • [^s] is a negative set which matches every character which is not in the set.


                • s matches a white space character (e.g. space, tab, etc.)


                • * matches 0 or more character of the preceding item




                Here is an example of how the browser checks if the pattern is correct (i.e. Google Chrome for example does not allow you to submit the form if there is a whitespace character in it. Test it here (enter a string containing a white space and hit Submit):






                <form>
                <input pattern="[^s]*">
                <button type="submit">Submit</button>
                </form>








                share|improve this answer






























                  0














                  You can use the following pattern:



                  <input pattern="[^s]*">




                  • [^s] is a negative set which matches every character which is not in the set.


                  • s matches a white space character (e.g. space, tab, etc.)


                  • * matches 0 or more character of the preceding item




                  Here is an example of how the browser checks if the pattern is correct (i.e. Google Chrome for example does not allow you to submit the form if there is a whitespace character in it. Test it here (enter a string containing a white space and hit Submit):






                  <form>
                  <input pattern="[^s]*">
                  <button type="submit">Submit</button>
                  </form>








                  share|improve this answer




























                    0












                    0








                    0







                    You can use the following pattern:



                    <input pattern="[^s]*">




                    • [^s] is a negative set which matches every character which is not in the set.


                    • s matches a white space character (e.g. space, tab, etc.)


                    • * matches 0 or more character of the preceding item




                    Here is an example of how the browser checks if the pattern is correct (i.e. Google Chrome for example does not allow you to submit the form if there is a whitespace character in it. Test it here (enter a string containing a white space and hit Submit):






                    <form>
                    <input pattern="[^s]*">
                    <button type="submit">Submit</button>
                    </form>








                    share|improve this answer















                    You can use the following pattern:



                    <input pattern="[^s]*">




                    • [^s] is a negative set which matches every character which is not in the set.


                    • s matches a white space character (e.g. space, tab, etc.)


                    • * matches 0 or more character of the preceding item




                    Here is an example of how the browser checks if the pattern is correct (i.e. Google Chrome for example does not allow you to submit the form if there is a whitespace character in it. Test it here (enter a string containing a white space and hit Submit):






                    <form>
                    <input pattern="[^s]*">
                    <button type="submit">Submit</button>
                    </form>








                    <form>
                    <input pattern="[^s]*">
                    <button type="submit">Submit</button>
                    </form>





                    <form>
                    <input pattern="[^s]*">
                    <button type="submit">Submit</button>
                    </form>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 17 '18 at 19:53

























                    answered Nov 17 '18 at 19:47









                    ssc-hrep3ssc-hrep3

                    5,18131554




                    5,18131554























                        0














                        The best way of addressing this problem is by writing the directive which you can use on multiple locations.



                        Here is the Stackblitz sample for the same






                        share|improve this answer






























                          0














                          The best way of addressing this problem is by writing the directive which you can use on multiple locations.



                          Here is the Stackblitz sample for the same






                          share|improve this answer




























                            0












                            0








                            0







                            The best way of addressing this problem is by writing the directive which you can use on multiple locations.



                            Here is the Stackblitz sample for the same






                            share|improve this answer















                            The best way of addressing this problem is by writing the directive which you can use on multiple locations.



                            Here is the Stackblitz sample for the same







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 17 '18 at 20:42

























                            answered Nov 17 '18 at 20:27









                            Kedar9444Kedar9444

                            38328




                            38328






























                                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%2f53354888%2fangular-form-input-block-space-regex%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)