-1

I have a text field to input tracking number that can have any length of numbers or alphabetic characters but no spaces, commas or special characters.I need the regular expression to match the above format.I have tried "/^[\w\s]+(?:,[^\s])+?$/" This validates "as," but not "as,," so odd number of commas are matched not even numbers.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • Have you tried something like: /^[a-z0-9]+$/i This should allow just numbers and letters, taken from http://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric – neilsimp1 Jun 22 '15 at 14:26
  • 1
    You say you do not want to match commas, but you have a comma in your regex; could you list a few examples of strings you would like to match and not match? – David Deutsch Jun 22 '15 at 14:28

3 Answers3

1

You can simply try ^[A-z0-9]+$ where [A-z] will take all the lowercase & uppercase alphabet & [0-9] will take just numbers.

You can test your regular expression at online regex tester

Sajid Rabbani
  • 361
  • 3
  • 5
0

If you really only want to match alphanumeric strings, the regex to use is /^[a-zA-Z0-9]+$/.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
0

Hi it's alphabet validation in this script

$('#textName').keypress(function (e) {
        var regex = new RegExp("^[a-zA-Z]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        }
        else
        {
        e.preventDefault();
        alert('Please Enter Alphabate');
        return false;
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="textName" />
Mansukh Khandhar
  • 2,542
  • 1
  • 19
  • 29