dweb Posted June 21, 2013 Share Posted June 21, 2013 hey all i have a html form and my input field looks like; <input id="fieldx" value="1,0,23,0,99"> the value changes each time, but that gives you an example As you'll see, I have 0's in the input field Inside a function, what i want to do is run the following var elm = document.getElementById('fieldx').value; then strip any 0's from that input field, and output the new value using document.form.fieldx.value = stripped_zeros; so the output would look like <input id="fieldx" value="1,23,99"> can anyone help? thanks Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/ Share on other sites More sharing options...
PravinS Posted June 21, 2013 Share Posted June 21, 2013 use javascript replace function var elm = document.getElementById('fieldx').value; elm.replace("0",""); after replacing "0" from string it will give output as "1,,23,,99", now replace ",," to blank elm.replace(",,",""); try this Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437152 Share on other sites More sharing options...
Psycho Posted June 21, 2013 Share Posted June 21, 2013 (edited) var elm = document.getElementById('fieldx').value + ','; elm = elmAry.replace('0,', ''); elm = elmAry.substr(0, elmAry.length-1) @Pravin, the problem with the process you proposed is that if the first value was a 0, then it would leave a comma at the beginning of the string. Edited June 21, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437154 Share on other sites More sharing options...
dweb Posted June 21, 2013 Author Share Posted June 21, 2013 use javascript replace function var elm = document.getElementById('fieldx').value; elm.replace("0",""); after replacing "0" from string it will give output as "1,,23,,99", now replace ",," to blank elm.replace(",,",""); try this Thanks, I tried that and ended up with function do_something() { var elm = document.getElementById('fieldx').value; elm.replace("0",""); elm.replace(",,",""); document.form.fieldx.value = elm; } but that didn't seem to work, any idea why? thanks Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437155 Share on other sites More sharing options...
Psycho Posted June 21, 2013 Share Posted June 21, 2013 but that didn't seem to work, any idea why? And what did it do? Any errors? Whya re you referencing the field by the ID at the beginning and then through the form object at the end? Try //Pass the field ID to the function function do_something(fieldID) { var fieldObj = document.getElementById(fieldID); var elm = fieldObj.value + ','; elm = elmAry.replace('0,', ''); elm = elmAry.substr(0, elmAry.length-1) fieldObj.value = elm; } //usage do_something('fieldx') Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437158 Share on other sites More sharing options...
codefossa Posted June 21, 2013 Share Posted June 21, 2013 (edited) This works .. str.replace(/0,|,0$/g, "") Edited June 21, 2013 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437159 Share on other sites More sharing options...
dweb Posted June 21, 2013 Author Share Posted June 21, 2013 var elm = document.getElementById('fieldx').value + ','; elm = elmAry.replace('0,', ''); elm = elmAry.substr(0, elmAry.length-1) @Pravin, the problem with the process you proposed is that if the first value was a 0, then it would leave a comma at the beginning of the string. thanks for fu And what did it do? Any errors? Whya re you referencing the field by the ID at the beginning and then through the form object at the end? Try //Pass the field ID to the function function do_something(fieldID) { var fieldObj = document.getElementById(fieldID); var elm = fieldObj.value + ','; elm = elmAry.replace('0,', ''); elm = elmAry.substr(0, elmAry.length-1) fieldObj.value = elm; } //usage do_something('fieldx') It just doesn't do anything, no errors, just dead, but i'll try your code above Question, do I do the following document.form.form_product_box_array2.value = fieldObj.value; or do I use document.form.form_product_box_array2.value = elm; thanks Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437160 Share on other sites More sharing options...
PravinS Posted June 21, 2013 Share Posted June 21, 2013 (edited) @dweb, the function given by Psycho should work now @Psycho, thanks for noticing the issue of "0" at first and last position Edited June 21, 2013 by PravinS Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437161 Share on other sites More sharing options...
dweb Posted June 21, 2013 Author Share Posted June 21, 2013 This works .. str.replace(/0,|,0$/g, "") nice one, that's pretty sweet. is there a way to make sure the value ends with a comma? and if not, add one on? Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437162 Share on other sites More sharing options...
codefossa Posted June 21, 2013 Share Posted June 21, 2013 (edited) nice one, that's pretty sweet. is there a way to make sure the value ends with a comma? and if not, add one on? That removes the value and comma that goes with it. But I did make one mistake. That will replace like "50,23" and leave "523". Try this. str.replace(/^0,|,0(,)|,0$/g, '$1') Edited June 21, 2013 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/279405-removing-0s-from-comma-seperated-value/#findComment-1437163 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.