cha0sriderx Posted February 18, 2008 Share Posted February 18, 2008 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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 19, 2008 Share Posted February 19, 2008 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> Quote Link to comment Share on other sites More sharing options...
cha0sriderx Posted February 20, 2008 Author Share Posted February 20, 2008 thanks alot for your help and it works. this is exactly how i wanted it, i wanted it to weed out the bad names. Quote Link to comment Share on other sites More sharing options...
cha0sriderx Posted February 20, 2008 Author Share Posted February 20, 2008 i was going to edit my post but i couldnt. anyways i wonder wondering how would i make the regexp also remove users that have 2 or more space in a row(ie: cha0s riderx). Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 20, 2008 Share Posted February 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
cha0sriderx Posted February 21, 2008 Author Share Posted February 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 21, 2008 Share Posted February 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.