0

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>

1 Answer 1

3
$('#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.

Sign up to request clarification or add additional context in comments.

2 Comments

@CodePitbull, don't forget to mark it as the correct answer if this solved your issue. That may help others with a similar problem in future.
Will do, but gotta wait 10 minutes more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.