admin管理员组

文章数量:1395159

I want to validate my username input box that does not allows any number or special characters inside. My Html

<div class="form-group">
  <input type="text" name="username" id="display_name" class="form-control input-lg"
  placeholder="User Name" tabindex="3">
</div>

Javascript Validation

username: {
  required: true,
  minlength: 5,
  maxlength: 15,
  remote: "ajax_val/val_username.php",
  notNumber: true 
 },

I want to validate my username input box that does not allows any number or special characters inside. My Html

<div class="form-group">
  <input type="text" name="username" id="display_name" class="form-control input-lg"
  placeholder="User Name" tabindex="3">
</div>

Javascript Validation

username: {
  required: true,
  minlength: 5,
  maxlength: 15,
  remote: "ajax_val/val_username.php",
  notNumber: true 
 },
Share Improve this question edited Apr 20, 2015 at 18:58 entropid 6,2395 gold badges35 silver badges45 bronze badges asked Apr 20, 2015 at 18:20 user4410715user4410715 1
  • 1 What's your question? – Dave Commented Apr 20, 2015 at 18:23
Add a ment  | 

2 Answers 2

Reset to default 6

You can use this regex:

var regex = /^[a-zA-Z]*$/;

DEMO

and if you dont want to use Regex then here is one approach:

function allowLetters(e, t) {
  try {
    if (window.event) {
      var charCode = window.event.keyCode;
    }
    else if (e) {
      var charCode = e.which;
    }
    else { return true; }
    return charCode > 64 && charCode < 91 || charCode > 96 && charCode < 123;
  }
  catch (err) {
    alert(err.Description);
  }
}

Here is one approach for HTML5 pattern attribute:

<form> 
    <input type='text' pattern='[A-Za-z\\s]*'/> 
</form>

I would take a look at regex something like [A-Za-z]+.

This may be helpful: (or any other guide on RegExp's on the internet)

https://developer.mozilla/en/docs/Web/JavaScript/Guide/Regular_Expressions

本文标签: javascriptInput validation for only alphabetical charactersStack Overflow