0

Not a lot of experience in RegEx stuff.

I have the following in java script which works perfectly fine.

The following pattern is used allow only alpha numeric

var valid = /^[A-Za-z0-9]+$/.test("a"); // returns true
var valid = /^[A-Za-z0-9]+$/.test("@"); // returns false

I am using the pattern part "^[A-Za-z0-9]" in some other places of the code and was asked to use the part "^[A-Za-z0-9]" in a variable and use it so that it is not repetitive. The following is a modification to the above:

var regExPart= "^[A-Za-z0-9]";
var regExString = ("/" + regExPart+ "+$/".replace(/\"/g, "")); // replacing the quotes
var regExp = new RegExp(regExString); // results in /^[A-Za-z0-9]+$/
var valid = regExp.test(charValue); // charValue could be any keyvalue "a" or "@"

//the above returns false for "a" 
//the above returns false for "@"

I am writing this in a keypress event to allow only alpha numeric

keypressValidation: function (e) {
var charCode = (e.which) ? e.which: event.keyCode;
var charValue = String.fromCharCode(charCode);
var valid = return /^[A-Za-z0-9]+$/.test(charValue);
if (!valid) 
{
    //prevent default (don't allow/ enter the value)
}

Not sure why. What am I missing in this. Need to return true for "a" and false for "@" for both the approaches. Any help/ suggestion would be of great help. Thank in advance.

5
  • I usually like DRY-ness, but IMO it's not something to try for across multiple separate regular expressions unless the repeated pattern is long. (also note that your character set simplifies to [a-z\d] with the i flag) Commented Oct 25, 2018 at 2:42
  • Could you provide what the content of charValue is ? Commented Oct 25, 2018 at 2:43
  • Also, do you only need to check if your character is a ? Why use a regex in that case ? Commented Oct 25, 2018 at 2:44
  • @Nicolas I am writing this as a part of keypress validation in java script to now allow user to enter only alpha numeric values. Updated my post. Commented Oct 25, 2018 at 2:49
  • Just use RegExp constructor correctly Commented Oct 25, 2018 at 2:58

3 Answers 3

1

For the RegExp class constructor, you do not need to specify forward slashes /.

var regExPart= "^[A-Za-z0-9]";
var regExp = new RegExp(regExPart + "+$"); // results in /^[A-Za-z0-9]+$/

console.log('a', regExp.test('a'))
console.log('@', regExp.test('@'))

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

Comments

1

It is not a must to contain '/'s in regexp

new RegExp("^[0-9a-zA-Z]$").test('a')

return true

new RegExp("^[0-9a-zA-Z]$").test('@')

return false

So just do

var rex="^[0-9a-zA-Z]$"

And you can use it anywhere. Tested in Chrome console.

Comments

0

I've made an example using your regex of what it should do, i think the way you were building your regex was not helping. You don't need to create a string and then create a new regex object , you can use /regex part/. Anyways here is a working example.

function keypress(e) {
  // Get the current typed key
  var keynum = e.key;
  // this regex only allow character between a and z and 0 and 9
  var regex = /^[a-zA-Z0-9]+$/;
  // we check if the current key matches our regex
  if(!keynum.match(regex) ) {
    // it doesn't ? well we stop the event from happening
    e.preventDefault();
  }
}
<input type="text" onkeypress="keypress(event)">

Comments

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.