Reputation: 75
Hi I have the following code, does anyone know how I can modify this so user can add spaces?
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
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