Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

extract numbers from a string and operate with them

Posted on 2004-04-14
10
Medium Priority
?
322 Views
Last Modified: 2009-07-29
hello all and thanks for reading this post.

i have a form with a text input, where the user can write the kind of teams where sorted on the game they played. but, i want to limit the "text" that the user is wrinting, cos the game is limited to 8 players, and i don't want them typeing something like 5vs6 or 7vs4 (game's max players numbers, how i said, is 8).

so, i though to take the string of the text input and scan the following things:
- it must be less than 4 characters in the string
- if there is no teams, the user must specify the number of players, just the number
- and finally, limit the number of players to 8

theres the code that i have:

function comprueba() {
      if(document.pSubir.pJugadoresOtro.value.length > 4) {
            alert("Error en el numero o equipos de jugadores\n\naoc-hispano.com");
            document.pSubir.siguiente1.value = "no";
      }
      if ((document.pSubir.pJugadoresOtro.value.length >= 1) && (document.pSubir.pJugadoresOtro.value.indexOf("vs") == -1)) {
            alert("Los equipos deben estar especificados siguiendo este formant:\n\n\t\t\tXvsY\n\nLo que significa que si es una partida de 3 contra 2, se debería indicar:\n\n\t\t\t3vs2\n\naoc-hispano.com");
            document.pSubir.siguiente1.value = "no";
      }
      
      var uno=document.pSubir.pJugadoresOtro.substring(0,1);
      var dos=document.pSubir.pJugadoresOtro.substring(0,4);
      
      if((document.pSubir.pJugadoresOtro.value.indexOf("vs") == 1) && (uno+dos > 8)) {
            alert("Contiene la cadena vs, y hay error en la cantidad de jugadores!");
            document.pSubir.siguiente1.value = "no";
      }
}

well, the two first "if" works correctly, but the third part of the function, its where i have problems to get it working.

surely that you will see some crazyness in this code, cos i just began to work in javascript last saturday..

the idea is to take the values of X and Y, from XvsY (4vs3, 2vs3, 1vs5...) and save them on a variable, called "uno" for X, and "dos" for Y. and test if uno+dos (X+Y) its bigger than 8.

if its bigger a window.alert will appear warning to the user that theres an error and the siguiente1.value will be assigned to "no".

i know the third part of the function its some kinda badly done, so do not think that u must be based on it

thanks all
0
Comment
Question by:paStiSSet
  • 4
  • 4
  • 2
10 Comments
 
LVL 7

Expert Comment

by:brgivens
ID: 10821192
Instead of using a single textbox, how about this?

<select>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
</select> &nbsp;vs&nbsp; <select>
  <option></option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
</option>
0
 
LVL 1

Author Comment

by:paStiSSet
ID: 10821378
good idea, but i want to do like i said
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10821408
Try this:

     var uno=parseInt(document.pSubir.pJugadoresOtro.substring(0,1));
     var dos=parseInt(document.pSubir.pJugadoresOtro.substring(0,4));

     if((document.pSubir.pJugadoresOtro.value.indexOf("vs") == 1) && (uno+dos > 8)) {
          alert("Contiene la cadena vs, y hay error en la cantidad de jugadores!");
          document.pSubir.siguiente1.value = "no";
     }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:paStiSSet
ID: 10821493
nope, it still not work.

there is a propiety like substring or something else to get a char from the position X?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10821573
Here my idea:

<html>
<head>
<script>
function setCount(theSel){
  unoOpt = theSel.form.uno.options;
  dosOpt = theSel.form.dos.options;
  selUno = theSel.form.uno;
  selDos = theSel.form.dos;
  optUno = selUno.options;
  optDos = selDos.options;
  valUno = selUno.value;
  valDos = selDos.value;
  equipCount = theSel.value;
  oldCount = optUno.length;
  if(equipCount>oldCount){
    for(i=oldCount+2;i<=equipCount;i++){
      optUno[i-2] = new Option(i, i);
      optDos[i-2] = new Option(i, i);
    }
  } else {
    optUno.length = equipCount-1;
    optDos.length = equipCount-1;
  }
}
function setOther(theSel){
  unoOpt = theSel.form.uno.options;
  dosOpt = theSel.form.dos.options;
  selUno = theSel.form.uno;
  selDos = theSel.form.dos;
  optUno = selUno.options;
  optDos = selDos.options;
  valUno = selUno.value;
  valDos = selDos.value;
  equipCount = theSel.form.numEquip.value;
  optUno.length = 0;
  optDos.length = 0;
  for(i=1;i<=equipCount;i++){
    if(i==valUno){
      optUno[optUno.length] = new Option(i, i, true, true);
    } else if(i==valDos){
      optDos[optDos.length] = new Option(i, i, true, true);
    } else {
      optUno[optUno.length] = new Option(i, i);
      optDos[optDos.length] = new Option(i, i);
    }
  }
}

</script>
</head>
<body>
<form name="pSubir">
Numero o Equipos:
<select name="numEquip" onChange="setCount(this)">
<option value="2" selected>2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
</select><br>
Play:
<select name="uno" onChange="setOther(this)">
<option value="1" selected>1
</select>
&nbsp;vs&nbsp;
<select name="dos" onChange="setOther(this)">
<option value="2" selected>2
</select>
</form>
</body>
</html>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10821586
Sorry, slightly reduced:

<html>
<head>
<script>
function setCount(theSel){
  optUno = theSel.form.uno.options;
  optDos = theSel.form.dos.options;
  equipCount = theSel.value;
  oldCount = optUno.length;
  if(equipCount>oldCount){
    for(i=oldCount+2;i<=equipCount;i++){
      optUno[i-2] = new Option(i, i);
      optDos[i-2] = new Option(i, i);
    }
  } else {
    optUno.length = equipCount-1;
    optDos.length = equipCount-1;
  }
}
function setOther(theSel){
  selUno = theSel.form.uno;
  selDos = theSel.form.dos;
  optUno = selUno.options;
  optDos = selDos.options;
  valUno = selUno.value;
  valDos = selDos.value;
  equipCount = theSel.form.numEquip.value;
  optUno.length = 0;
  optDos.length = 0;
  for(i=1;i<=equipCount;i++){
    if(i==valUno){
      optUno[optUno.length] = new Option(i, i, true, true);
    } else if(i==valDos){
      optDos[optDos.length] = new Option(i, i, true, true);
    } else {
      optUno[optUno.length] = new Option(i, i);
      optDos[optDos.length] = new Option(i, i);
    }
  }
}
</script>
</head>
<body>
<form name="pSubir">
Numero o Equipos:
<select name="numEquip" onChange="setCount(this)">
<option value="2" selected>2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
</select><br>
Play:
<select name="uno" onChange="setOther(this)">
<option value="1" selected>1
</select>
&nbsp;vs&nbsp;
<select name="dos" onChange="setOther(this)">
<option value="2" selected>2
</select>
</form>
</body>
</html>

0
 
LVL 1

Author Comment

by:paStiSSet
ID: 10821659
hi again
thanks brgivens and grettings Zvonko

zvonko, your idea is not what i am looking for, its different, but a good idea...

i have tested this one and looks fine. but it does not do what im asking for (im agree with your idea), so how about two selects how brgivens said, but, when selecting for example 2 on the first select, it will change the options automatically in the second select, doing that the second select only can have 6 options.. for example:

case 1:
----------------------------------------------
select1 [  3  ]               select2 [  1  ]
                                             [  2  ]
                                             [  3  ]
                                             [  4  ]
                                             [  5  ]
select1 value is 3, now, select2 can only have 5 values (3+5=8)

----------------------------------------------

case 2:
----------------------------------------------
select1 [  5  ]               select2 [  1  ]
                                             [  2  ]
                                             [  3  ]
select1 value is 5, now, select2 can only have 3 values (5+3=8)
----------------------------------------------


this idea will work perfectly on my form :)
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 10821830
Like this:

<html>
<head>
<script>
function setCount(theSel){
  equipCount = theSel.value;
  optDos = theSel.form.dos.options;
  optDos.length=0;
  for(i=1;i<=8-equipCount;i++){
    optDos[i-1] = new Option(i, i);
  }
}
</script>
</head>
<body>
<form name="pSubir">
<select name="numEquip" onChange="setCount(this)">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7" selected>7
</select>&nbsp;&nbsp;&nbsp;
<select name="dos">
<option value="1" selected>1
</select>
</form>
</body>
</html>

0
 
LVL 1

Author Comment

by:paStiSSet
ID: 10821839
IT'S PERFECT!! Lotta thanks Zvonko ;D
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10823970
You are welcome.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question