-2

I am new to javascript and I have a requirement that input field can take input of 'AND' or 123 or ().

I have tried RegExp

function requiredletter(inputtxt) { 
    var letters =/\a+\n+\d/;
    if(inputtxt.value.match(letters)) {
      alert('Your name have accepted : you can try another');
      return true;
    } else {
      alert('Please input alphabet characters only');
      return false;
   }
}

Its not reflecting the desired result.

Sam-dev-sfdc
  • 33
  • 1
  • 8
  • 2
    Do you mean specifically ABC or 123? or any combination of BCA/AGR or 142/451/323? – MyLibary Jun 21 '19 at 12:32
  • We're going to need more information. What exactly do you want and how are you calling `allLetter` and what is the "desired result"? – Liam Jun 21 '19 at 12:33
  • input can be abc,bca,acb ,213,123,321,)(,() or any combination but it should not have any other letters. – Sam-dev-sfdc Jun 21 '19 at 12:40
  • [Have you thought about using a Switch statement?](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) –  Jun 21 '19 at 12:41
  • Then what about `a1(2c)3` or something. Is that acceptable too? – Theo Jun 21 '19 at 12:50
  • `return /^[abc123()]+$/.test(s);` will return true if the input only contains 1+ of the mentioned chars. – Wiktor Stribiżew Jun 21 '19 at 13:10

5 Answers5

2

I have no idea what you're asking anymore. Here is a string as whole.

let regex = /^[a-c]{3}[1-3]{3}[\)\(]{2}$/;

console.log(regex.test('abc123)(')) // true
console.log(regex.test('abc123()')) // true
console.log(regex.test('bca123()')) // true
console.log(regex.test('acb231)(')) // true
console.log(regex.test('bac213()')) // true
console.log(regex.test('cab321)(')) // true
console.log(regex.test('abcd123()')) // false
  • This would accept `a1b2bc1` which is not what the OP has asked for *'ABC' **or** 123* – Liam Jun 21 '19 at 12:31
  • I mis-read the question and assumed he only wanted alpha numeric characters. –  Jun 21 '19 at 12:32
  • That's still wrong. Really this question needs clarifying, adding an answer now would be very much jumping the gun – Liam Jun 21 '19 at 12:33
  • 2
    Sir Duke is one of my favs Mr wonder BTW – Liam Jun 21 '19 at 12:35
  • I think this will break if you enter abc123 – Kobe Jun 21 '19 at 13:04
  • 1
    I just don't think it's clear enough and I'm very close to just deleting my answer and forgetting this question was every asked. I've asked a couple times with no reply, I don't know if you can enter "a", "b", "c" as seperate answers or "abc", "bac", "123", "321" as seperate answers or a whole string of "cab231)(" as an answer. –  Jun 21 '19 at 13:06
  • Yeah i get you, I still don't know what op wants lol – Kobe Jun 21 '19 at 13:12
1

Use this regex:

ex = /^[a-c]{3}[1-3]{3}[()]{2}$/

const log = e => console.log(e)

const test = s => ex.test(s)

log(test('abc123()')) // true
log(test('bca132)(')) // true
log(test('abce123()')) // false
log(test('abc1234()')) // false
log(test('abc123)(')) // true
log(test('abc123(')) // false

It will return true if the prompt fits the criteria, otherwise it will return false.

Kobe
  • 6,226
  • 1
  • 14
  • 35
  • Doesn't work with like `bac` or `cab`, apparently it can be any combination of a, b, c or 231, 312, 321 or ( or ) or () or )(. The question really needs clarifying. –  Jun 21 '19 at 12:43
  • @swonder It works with capitals, which OP has requested – Kobe Jun 21 '19 at 12:44
  • Yeah but he said input can be abc,bca,acb ,213,123,321,)(,() in his comment.. I'm not saying it's wrong because it's capitals only it works fine just OP not explained his question well enough. –  Jun 21 '19 at 12:44
  • @swonder ahh, thanks, i missed that – Kobe Jun 21 '19 at 12:45
  • 1
    @NickParsons that also flew over my head, ill edit that in, thanks. – Kobe Jun 21 '19 at 12:46
  • for example if i input abc123() it will be fine but if i enter abce123() it should throw an error – Sam-dev-sfdc Jun 21 '19 at 12:48
  • because of the length? @Sam-dev-sfdc – Kobe Jun 21 '19 at 12:49
  • 1
    @Sam-dev-sfdc can you go an edit your question? Be very clear, can I enter 1 letter? 1 letter, 1 number? Do I ender abc123() or do I enter 3 different times with abc, 123 and (). Help us help you. –  Jun 21 '19 at 12:49
  • 1
    @Kobe just spotted as well, instead of [a-z] would be [a-c] –  Jun 21 '19 at 12:52
  • 1
    @swonder thank you, I'm missing a lot since it's so vague :P – Kobe Jun 21 '19 at 12:53
  • alphabet should be in abc like bca,acb range or numeric value should be in 123,321,213 or parenthesis can be of )(,().hope you get the question – Sam-dev-sfdc Jun 21 '19 at 12:54
  • @Sam-dev-sfdc does it have to be all in one go? or separate statements? – Kobe Jun 21 '19 at 12:55
  • So just to clarify there is always going to be an answer with 3 letter (abc), a separate answer with 3 numbers (123) and a final answer of parenthesis both standard and inverted? () )(? –  Jun 21 '19 at 12:56
  • it can be separate like you enter only abc it would be okay but abcd will not be. – Sam-dev-sfdc Jun 21 '19 at 12:59
  • I still don't think it's clear lol. Can I submit the answer "a" and you want it to return true? Or do I have to submit "abc" as a whole string? –  Jun 21 '19 at 13:04
  • any alphabet or digit other than abc 123 () will not accepted – Sam-dev-sfdc Jun 21 '19 at 13:09
  • @Sam-dev-sfdc try mine now – Kobe Jun 21 '19 at 13:28
  • @Kobe don't forget your ^ for the start of the string and $ for the end of the string, You could do `FOOabc123()BAR` and it would still pass. –  Jun 21 '19 at 13:39
  • @swonder I don't think it would - I added a check against the original string, but that sounds like a better solution, thank you. – Kobe Jun 21 '19 at 13:42
  • [Example here](https://regexr.com/4g7g0) –  Jun 21 '19 at 13:44
  • @swonder I mean I added a javascript check, if you check this [fiddle](https://jsfiddle.net/Kozandmo/k8pj6xba/2/), you'll see what i mean, but I've edited it out now. – Kobe Jun 21 '19 at 13:48
  • Ah yes, you're absolutely right my apologies :) –  Jun 21 '19 at 13:48
  • 1
    No worries, lets hope we helped OP at least a little bit :P – Kobe Jun 21 '19 at 13:49
  • @Kobe I don't know what OP wants now.. see his comment on my answer. He specifically said 'bc123()' will be accepted. But your regex won't accept that.. can you take a look at my second answer and tell me if you think I got it right!!! Cause the OP has abandoned me now :( !! (Just kidding!! :) – prime_hit Jun 21 '19 at 14:45
0
const regex = /^(ABC|123|\(\))$/;

regex.test('ABC'); // true
regex.test('123'); // true
regex.test(123);   // true
regex.test('()');  // true

regex.test('abc'); // false
regex.text(213);   // false
regex.test(')(');  // false
ankr
  • 647
  • 6
  • 15
0

If you want all the possible permutation of letters from abc , 123 and () , and that they are not allowed to be together, then you can use following regex:

var reg = /[abc]{3}|[123]{3}|[()]{2}/;
//This will match aaa, abc, bbb, bca, ccc, 123, 213, (), )( , etc...

https://regex101.com/r/44YoW8/2

Based on OP's comment in @Kobe's answer, (s)he wants any combination of abc , 123 and (). You can use the following regex if you just want these kind of characters, namely a, b, c, 1, 2, 3, (, and ) with any number of these letters, you can use :

/[abc123()]*/

For checking if the given string matches the regex

To test, you need to test whether the returned value of the match function is equal to the actual string. Consider following code:

test1 = "abcd";
test2 = "abc";
reg = /abc/;
if (test1 == test1.match(reg)) console.log("test1 matched"); //it won't be printed as it doesn't matches completely

if (test2 == test2.match(reg)) console.log("test2 matched"); //this would be printed.

But, if you want permutation of these letters, numbers and symbols without change in number of kind, then I don't think regex will help. See Regex permutations without repetition. But you can write a simple function for this checking easily.

prime_hit
  • 326
  • 1
  • 7
  • it is working fine but if i am entering abce123() it is accepting it also – Sam-dev-sfdc Jun 21 '19 at 13:07
  • No, it is not accepting abce123(). And, actually, the first regex won't even accept `abc123()`. When you will apply the match function on the string, regex uses greedy approach to match as match as it can and returns the string. Therefore, `"abc123()".match(reg)` will return `abc` as a match. What you need to do is to check whether the returned value is equal to actual string. E.g., `test = "abce"; reg = /[abc]{3}|[123]{3}|[()]{2}/; if (test == test.match(reg)) console.log("accepted!"); else console.log("rejected");` – prime_hit Jun 21 '19 at 13:13
  • @Sam-dev-sfdc I updated answer to clarify you how you test whether the given string matches or not with a given regex. – prime_hit Jun 21 '19 at 13:20
  • i tried this test = "abc123()"; reg = /[abc]{3}|[123]{3}|[()]{2}/; if (test == test.match(reg)) console.log("accepted!"); else console.log("rejected"); VM1392:1 rejected.this is not as per the requirement. – Sam-dev-sfdc Jun 21 '19 at 13:25
  • As I stated, it won't be accepted by first regex, but it will be accepted by second regex. But the second regex will accept everything like `aaaaaaa`, `abc123()()()`, `abc111((())((`, etc. As other's have stated, it is hard to understand what you actually want to accept and what to reject. Initially I though you want to accept only the permutation of `abc` , `123`, and `()`, but then you said in your comment that abc123() is also accepted. If you can answer my questions, I can perhaps help you: Which of the following will be accepted: abcccc, abc123, abc123(), abc123211221()))((, aaaa,.. etc. – prime_hit Jun 21 '19 at 13:30
  • contd 1., abcabc, abc123123()(), ... Is there limitation on number of characters? Is abc123() characters are allowed to repeat? Can they be left out in a string, that is whether just `a` will be accepted? – prime_hit Jun 21 '19 at 13:31
  • i don't need any digit other than the required any input like abc,abc123,abc123(),bca,bc123,() or further will be acceptable.hope you understand my requirement now. – Sam-dev-sfdc Jun 21 '19 at 13:35
  • @Sam-dev-sfdc Please see the new answer I added. It will satisfy all your requirements hopefully! – prime_hit Jun 21 '19 at 13:55
0

Based on the following comment by OP I don't need any digit other than the required any input like abc,abc123,abc123(),bca,bc123,() or further will be acceptable.hope you understand my requirement now. I am adding a new answer for this question.

As far as I can understand, the OP wants to accept any permutation of the characters from the string abc123(). We can choose any number of character from this string, but we cannot repeat the characters. Therefore, everything from "a", "b", "c" to "abc", "bca", "123()", "(", ")", ")(" ... etc will be acceptable.

A regex won't work in this case because regex can't remember stuff like which characters are used and which cannot be used anymore, and hence I think following function will work for this work (might not be very efficient algorithmically):

function checkString(str){
      main_str = "abc123()";
    
      for (var i = 0; i<str.length; i++){
        chr = str[i];
        chr1 = chr; //make a copy of character
        if (chr == "(" ||  chr== ")") chr1 = "\\" + chr;
        if (main_str == "") return false;
        if (main_str.match(chr1)) main_str = main_str.replace(chr,''); //remove that character from main_str
        else return false;
      }
      return true;
    
    }
 
//Following are True
console.log(checkString("abc123()"));
console.log(checkString("abc"));
console.log(checkString("bca)("));
console.log(checkString("()"));
console.log(checkString("213"));
console.log(checkString("ab12)"));

//Following are False
console.log(checkString("abc123()a"));
console.log(checkString("abc123()()"));
console.log(checkString("abcd123()"));

What this function does is that it test whether all the characters in the given string are in the abc123() and there is no repetition of character. Therefore, abc, a, abc123() will be accepted, but not abcd, aa, or abc1233... etc.

I hope this answers your question.

prime_hit
  • 326
  • 1
  • 7