Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

extract numbers from a string and operate with them

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
paStiSSet
Asked:
paStiSSet
  • 4
  • 4
  • 2
1 Solution
 
brgivensCommented:
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
 
paStiSSetAuthor Commented:
good idea, but i want to do like i said
0
 
brgivensCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
paStiSSetAuthor Commented:
nope, it still not work.

there is a propiety like substring or something else to get a char from the position X?
0
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
paStiSSetAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
paStiSSetAuthor Commented:
IT'S PERFECT!! Lotta thanks Zvonko ;D
0
 
ZvonkoSystems architectCommented:
You are welcome.
0

Featured Post

Technology Partners: 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!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now