...

View Full Version : regex validation



beginner_18
09-24-2012, 08:45 AM
Hi all,
can anyone give an example of regex which should not accept duplicate numbers separated with comma.
it should accept numbers like 1,2 and it should not repeat duplicates like 1,2,3,1,2,3
and also the number should terminate with a number like 1,2,3 and it should not terminate with , like 1,2, please help me

Philip M
09-24-2012, 10:52 AM
I don't think that a single regex can do this. You need something like:-


<html>
<head>
</head>

<body>

<script type = "text/javascript">

var val = "1,2,3,1,2,3,1,5,3,8,";
val = val.replace(/\,$/,""); // strip final comma if any
sval = val.split(","); //split string into an array

for (var i = 0; i<sval.length; i++) {
for (var j = i+1; j<sval.length; j++) {

if (i!=j) {
if ((sval[i] == sval[j]) && sval[i] !="") {
alert ("The value " + sval[i] + " array index " + i + " is duplicated at array index " + j);
sval[j] = ""; // delete duplicates
}
}

}
}

// ---------------------------------------------------------

var sval2 = [];
var j=0;
for (var i = 0; i<sval.length; i++) {
if (sval[i] != "" ) { // remove blanks (were duplicates) from the array
sval2[j] = sval[i];
j++;
}
}

var final = sval2.join(",");
alert (final);

</script>

</body>
</html>

Quizmaster: The Spanish Steps and the Colliseum lie in which Italian city?
Contestant: Athens.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum