2

Hello I have a scenario that, in textbox I can enter string like

"1,2,3" this would be allowed.

but if someone enters,

"1,2,,3" this would not be allowed.

allowed multiple commas but not like above.

Thanks in advance

7 Answers7

3

Try this reg-ex:

/^\d(?:,\d)*$/

Explanation:

/            # delimiter
  ^          # match the beginning of the string
  \d         # match a digit
    (?:      # open a non-capturing group
      ,      # match a comma
      \d     # match a digit
    )        # close the group
    *        # match the previous group zero or more times
  $          # match the end of the string
/            # delimiter

If you allow multi-digit numbers, then change \d to \d+.

Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
0

Try this,

if(in_array("", explode(',',$str)))
{
    // validation fail
}
viral
  • 3,724
  • 1
  • 18
  • 32
  • The problem with this is it's a bit overkill. You're splitting the string just to see if you have an empty spot. It works, but it's using a sledgehammer to drive a nail. – samanime May 30 '15 at 07:44
  • Yes, i agree with you, but OP is validating this for a checkbox, IMO we dont need to worry until OP has 10000 visitors at a given time, check [this](http://stackoverflow.com/a/13483548/3113793) answer, it says it took `1.738441 seconds` to perform 10000 iterations, we need only one here, so performance should not be considered here. Guide me if im wrong. – viral May 30 '15 at 09:01
0

You can simply do a regex test to check. If the only thing you want to prevent is duplicate commas:

if (preg_match('/,,/', $myString)) {
    // not allowed... do something about it
}

If you want to limit it to only a pattern of numbers, separated by commas, swap the regex pattern for '/^([0-9]+,?)+$/', which only 1 or more numbers, optionally followed by a decimal, with that pattern repeated any number of times (but must have at least one number). Also, flip the conditional around, so:

if (!preg_match('/^([0-9]+,?)+$/', $myString)) {
    // not allowed... do something about it
}

If you want something a little simpler, doing this will also solve it (and be a little more efficient, if all you want is to test for multiple commas together):

if (strpos($myString, ',,') !== false) {
    // not allowed... do something about it
}
samanime
  • 25,408
  • 15
  • 90
  • 139
0

Try This:

if (strpos($input_string,',,') == true) {
    echo 'Invalid string';
}
Sagar
  • 642
  • 3
  • 14
  • If I'm not mistaken, a string of ",," would not be detected as invalid. Whether that's a use-case that the OP is considering or not is questionable, but it'd miss that since the index would be 0, which is not true. – samanime May 30 '15 at 07:45
0

You can detect this using (preg_match will work too of course):

if(strpos($your_string, ',,') !== false) {
   echo "Invalid"
}

Do you also need to detect leading or trailing commas? Also keep in mind if validation is not really necessary you can simply "fix" the input by using explode and filtering out empty string elements, and then implode array:

$your_string = implode(',', array_filter(explode(',', $your_string), function ($i) {
    return $i !== '';
}));
vakata
  • 3,726
  • 1
  • 17
  • 31
0

use the strpos() function for your above requirement

 if (strpos($youstring,',,') == false) {
        echo 'String not found';
    }
    else
    {
        echo 'String is found';
    }
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
0

You can use stristr function to fix this

if(stristr ($Array,',,'))
echo 'Flase';
else
// do something
sujivasagam
  • 1,659
  • 1
  • 14
  • 26