HEEN
HEEN

Reputation: 4721

Regex to allow space after one word

I have used a regex which allows the user to enter alphanumeric values like

ABC123, xyz321

but now what I want is with these characters, it is not allowing me to enter space between some characters.

abc test7, 123test abc4 this should also work

Working Example:-

123abc test4

A7 organisation.

I dont want like this:-

1221 121

below is my code

function NumbersWithCharValidation(thisObj) {
        var textBoxvalue = thisObj.value;
        if (textBoxvalue.length == 0 || (isNaN(textBoxvalue) && !textBoxvalue.match(/\W/))) {
        }
        else {
            alert('Numbers and Special characters are not allowed');
            document.getElementById('txtNameOfFirm').focus();
        }
    }

kindly suggest what is wrong here

Upvotes: 2

Views: 374

Answers (2)

Taran J
Taran J

Reputation: 805

use this regEx to match ur input:

(?!\d+\b)\w+\s*

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use this regex with negative lookahead:

/^(?!\d+\b)\w+(?: \w+)*$/

This regex will spaced between words. Negative lookahead (?!\d+\b) is to prevent the case of 123 456 as valid input.

RegEx Demo

Upvotes: 2

Related Questions