How can I trim a string i.e. remove leading and trailing spaces ? I have a form validation which requires the user to specify a value, i.e. not leave the field blank. I want to ensure that the user cannot simply press the Spacebar a few times and leave the field.
aparnesh
9
Junior Poster
Recommended Answers
Jump to Postas an evil one-liner:
<input onBlur="alert(this.value.match(/[\w\d]/)!=null?'OK':'NOT OK');">
or more useably:
function check_field(ident){ var target = document.getElementById(ident); return target.value.match(/[\w\d]/) != null; } function check_form(){ if(check_field('the_one_that_can't_be_spaces')){ //Everything's fine! }else{ //Something's missing o_O } }
if the String.match(/regex/) function returns a valid match for \w (any alphabetic …
All 3 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.