2

I just can't get this figured out. I'm using jquery.validator.addMethod. Currently I have the following:

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, and underscores only please");

This allows letters, underscores and numbers. But what I'd need to allow instead of underscore would be a dash (-).

I'm very new to regex and I've been trying to figure out this simple problem but I just can't find a way to do it. Any help would be much appreciated.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
EmJeiEn
  • 1,343
  • 5
  • 17
  • 30
  • Do you really need to add a method for this? I think you just need to specify the rule: `pattern: /^[a-z0-9\-]+$/i` and the message: `pattern: "Letters, numbers, and underscores only please"`. – PeterKA Jun 21 '15 at 22:16
  • If there's another way, I didn't know about it. Going to look into it straight away :) – EmJeiEn Jun 21 '15 at 22:18
  • 1
    I have added an answer just in case you find that approach useful in the future. – PeterKA Jun 21 '15 at 22:37
  • Indeed, much simpler like that. Thank you, I will use that in the future. – EmJeiEn Jun 21 '15 at 22:42

2 Answers2

2

It's called "dash" try this:

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
}, "Letters, numbers, and dashes only please");
michelem
  • 14,430
  • 5
  • 50
  • 66
  • Thank you! That seems to accept everything though now that I test it. signs like: @_ etc are going through as well. – EmJeiEn Jun 21 '15 at 22:07
  • Please consider to accept the answer (http://stackoverflow.com/help/someone-answers) if it solves your problem. – michelem Jun 21 '15 at 22:12
  • I'd accept with pleasure :) But like I said, it doesn't work. It doesn't limit it to the letters, numbers and dashes only, instead whatever symbol is accepted as it is. For example @ _ – EmJeiEn Jun 21 '15 at 22:16
  • Try to return only true/false with the regex: `return /([a-z0-9\-]+)/i.test(value);` – michelem Jun 21 '15 at 22:22
  • Used this one instead and it works: /^[a-z0-9\-\s]+$/i.test(value); Found here: http://stackoverflow.com/questions/11326910/jquery-validator-validate-alphanumeric-space-and-dash if you could edit it to the answer, I'll accept it. – EmJeiEn Jun 21 '15 at 22:24
1

You could achieve the same result by specifying a pattern rule and message as follows:

$('form').validate({
    rules: {
      alphanumdash: {
        required: true,
        pattern: /^[a-z0-9\-]+$/
      }
    },
    messages: {
      alphanumdash: {
        required: 'AlphaNumDash is required',
        pattern: 'Letters, numbers, and dashes only please'
      }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/additional-methods.min.js"></script>
<form id="test">
  <input type="text" name="alphanumdash" /><br/>
  <input type="submit" />
</form>
PeterKA
  • 24,158
  • 5
  • 26
  • 48