Jump to content

validating form


cha0sriderx

Recommended Posts

Im trying to scan through a textarea to find invalid names and then remove them.  invalid names are ones that have any character that isnt a number, letter or space, but the space cant be at the beginning or end.

 

ex: player1,player2,player3 - valid

ex: player1, player2, player3 - should remove " player2" and " player2"

 

<script type="text/javascript">
  function testnames() {
    exp=/^[a-zA-Z0-9]+[a-zA-Z0-9\ ]+[a-zA-Z0-9]{1,}$/;
    box = document.forms.test.users.value;
    box2 = box.split("\,");
    for (i=0; box2[i].length > 0; i++) {
      x = exp.test(box2[i]);
      if (!x) {

      }
    }
  }
</script>

Link to comment
Share on other sites

Would you not want to trim the leading and trailing spaces off? Anyway, here is what you asked for:

<html>
<head>
  <script type="text/javascript">

function validate(inputStr) {

  var inputAry = inputStr.split('\,');
  var outputAry = new Array();

  validName = /^[a-z0-9]+[a-z0-9\ ]*[a-z0-9]+$/i

  for(var i=0; i<inputAry.length; i++) {
    if (validName.test(inputAry[i])) {
      outputAry[outputAry.length] = inputAry[i];
    }
  }

  var outputStr = outputAry.join(',');

  return outputStr;

}

  </script>
</head>

<body>

Input:<br>
<textarea id="input">player1, player2, player3,player4</textarea><br><br>

Input:<br>
<textarea id="output"></textarea><br><br>

<button onclick="document.getElementById('output').value=validate(document.getElementById('input').value);">Test</button>

</body>
</html>

Link to comment
Share on other sites

I really like helping people out, but it irks me when someone gives requirements for a solution they need, then when provided a solution, they change the requirements. I don't meant to be a jerk, but it seems rude to me when someone does that. I'm sure that was not your intention, but it is always appreciated to get the full requirements up front instead of usign the forum for design.

 

validName = /^[a-z0-9]+([\ ]?[a-z0-9])*$/i

Link to comment
Share on other sites

it wasnt my intentions to be a jerk about it, i was just asking how would i change just the exp to fit the new requirements.  i really do appreciate your help, i just figured it was easier to reply to this post instead of making a completely new one.

 

You were not being a jerk (and it was not my intention to be one either). I just wanted to emphasize that stating ALL of your requirements up front prevents a lot of rework and gets you a solution faster. It would ahve been just as much work for me to provide all of your requirements instead of having to go back and rework it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.