-1

I want to check whether my userid is having any special character apart from alphanumeric characters (a-z,A-Z,0-9). The below regex is failing...what would be the correct way?

var testStr = '3278|3278.3000.2506.60200000.1100.000.0000.0||703"68*[70"8';
var splitTeststr = testStr.split('|');
alert(splitTeststr[0]);
/* alert('Length of splitStr is : ' +splitTeststr.length); */
if (splitTeststr.length == 4) {
    var userId = splitTeststr[3].trim();
    alert('userId before removing junk : ' +userId);
    var letterNumber = /^[0-9a-zA-Z]+$/;
    
    if((userId.match(letterNumber))){
        alert('UserId contains junk : ' +userId);
        
    }else{
        alert('Userid is fine');
    }
}

3
  • It seems that your regex is correct but your condition is reversed Commented Jul 29, 2020 at 11:03
  • This is not about you, but I wonder why in 2020 people keep using var and alert. This is so recurring on SO that I'm starting wondering if they're all from the same source Commented Jul 29, 2020 at 11:03
  • @CristianTraìna- Our legacy application will support in this way only. Oracle product :) Commented Jul 29, 2020 at 11:13

1 Answer 1

0

There is an issue in your regex, the correct one is /[^0-9a-zA-Z]/:

function testUserId(userId){
    console.log('userId before removing junk : ' +userId);
    var letterNumber = /[^0-9a-zA-Z]/;
    if((userId.match(letterNumber))){
        console.log('UserId contains junk : ' +userId);
    }else{
        console.log('Userid is fine');
    }
 }

// letter and numbers
testUserId("34293483209jfeoiAAAAsojf");
// numbers
testUserId("1234");
// letters
testUserId("aaaBBBBB");
// rejected
testUserId("aa!aBBB!..BB");

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

1 Comment

@Greedo- Thanks. Yeah...i too just figured it out. Cool buddy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.