Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 07:45 AM   PM User | #1
beginner_18
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
beginner_18 is an unknown quantity at this point
regex validation

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
beginner_18 is offline   Reply With Quote
Old 09-24-2012, 09:52 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I don't think that a single regex can do this. You need something like:-

Code:
<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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 09-24-2012 at 01:03 PM.. Reason: Revised
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:29 AM.


Advertisement
Log in to turn off these ads.