CodePitbull
CodePitbull

Reputation: 75

Unable to add spaces to input text

Hi I have the following code, does anyone know how I can modify this so user can add spaces?

JSFiddle

I used his code and modified it here:

$('#text').keyup(function(e, w) {
            $("#prev").html($(this).val());

            var txtwidth = $( "#text-preview" ).width();
            $( "#textWidth" ).text( "Approx. Width: " + txtwidth + " px." );
        }).keypress(function(e) {
            return /[a-z0-9.-]/i.test(String.fromCharCode(e.which));
        });

HTML:

                          <!--display user input-->
                          <span id="text-preview"><p id="prev" class="form_result"></p></span>
                      <!--display width-->
                      <p id="textWidth"></p>
                          <label class="sign-text">Enter your text
                            <input type="text" name="text" id="text" class="form-control enter-text-field validation-passed" value="Enter Your Text">
                          </label>

Upvotes: 0

Views: 2002

Answers (1)

Stefan Kendall
Stefan Kendall

Reputation: 67802

$('#text').keyup(function(e, w) {
            $("#prev").html($(this).val());

            var txtwidth = $( "#text-preview" ).width();
            $( "#textWidth" ).text( "Approx. Width: " + txtwidth + " px." );
        }).keypress(function(e) {
            return /[a-z0-9.-\s]/i.test(String.fromCharCode(e.which));
        });

The regex in the keypress function needs \s to support space characters.

Upvotes: 3

Related Questions