Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

extract numbers from a string and operate with them

Posted on 2004-04-14
10
Medium Priority
?
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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'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.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

670 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