Jason28 Posted September 22, 2008 Share Posted September 22, 2008 Hello, I would like to use javascript to locate two asterisks side by side ** and replace them with only one asterisk before submitting the form. So if someone enters "blah**" or "**blah" or just "**" it will remove one of those asterisks. Here are my current form fields: <input name="search_user" id="search_user" type="text" size="45" maxlength="25" value="{search_user}"> <input name="search_musician" id="search_musician" type="text" size="45" maxlength="100" value="{search_musician}"> <input name="search_song" id="search_song" type="text" size="45" maxlength="255" value="{search_song}"> If you could provide a working example of javascript to replace ** with * that would be really great Thanks! Link to comment https://forums.phpfreaks.com/topic/125250-solved-use-javascript-to-replacestrip-out-character/ Share on other sites More sharing options...
Psycho Posted September 22, 2008 Share Posted September 22, 2008 You don't state what should happen if there are three or more asterisks in a row! I will assume that any multiple succession of asterisks (2 or more) should be replaced with a single asterisk. Here's a sample script: <html> <head> <script type="text/javascript"> function removeAst(fieldObj) { fieldObj.value = fieldObj.value.replace(/\*{2,}/g, '*'); } </script> </head> <body> Enter a value and tab out:<br> <input type="text" name="theField" onchange="removeAst(this);" /> </body> </html> Link to comment https://forums.phpfreaks.com/topic/125250-solved-use-javascript-to-replacestrip-out-character/#findComment-647489 Share on other sites More sharing options...
Jason28 Posted September 22, 2008 Author Share Posted September 22, 2008 Thanks I will try it now Yeah I do not think 3 asterisks will be a common problem but two are Link to comment https://forums.phpfreaks.com/topic/125250-solved-use-javascript-to-replacestrip-out-character/#findComment-647502 Share on other sites More sharing options...
Jason28 Posted September 22, 2008 Author Share Posted September 22, 2008 Haha works great thanks a lot I am a php guy I do not know how you guys know javascript it seems more difficult. I tried reading a book on it but it must have sucked since I still haven't learned how to create custom codes like the one you have provided. Link to comment https://forums.phpfreaks.com/topic/125250-solved-use-javascript-to-replacestrip-out-character/#findComment-647504 Share on other sites More sharing options...
Psycho Posted September 22, 2008 Share Posted September 22, 2008 Personally I think JavaScript and PHP are VERY similar. The function simply accepts a form field object. The function then replaces the value of that form field with the same value run through a bit of RegEx code. A PHP implementation would look similar: (not tested) $cleanValue = preg_replace('(/\*{2,}/', '*', $_POST['fieldname']); ' In fact, it is fine to have this functionality in JavaScript, but you should really be doing it on the server-side (i.e. in PHP) as users may have JavaScript disabled. Link to comment https://forums.phpfreaks.com/topic/125250-solved-use-javascript-to-replacestrip-out-character/#findComment-647813 Share on other sites More sharing options...
Jason28 Posted September 22, 2008 Author Share Posted September 22, 2008 Yes I have everything done server side already with php and this time I am also using javascript to keep down the server load and to not annoy visitors by displaying a simple popup instead of reloading a page with an error. Link to comment https://forums.phpfreaks.com/topic/125250-solved-use-javascript-to-replacestrip-out-character/#findComment-648045 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.