October 10, 200817 yr Hi everyone. On my website im using the script below to check that a form has been filled out correctly. However the script is not bringing up the alert when a character other than a letter or number is entered in the "username" field. Iv had a play around and i beleive it is because when the correct format is used to fill out the "password" field, it is returning true, stopping the rest of the script from running. Does anybody have any ideas how to fix this problem? Iv tried removing the return true; value from the password field but that does not work. My javascript knowlege is very limited so i dont know how to proceed any further. All help is greatly appreciated. Many thanks. <script type="text/javascript"> function doPasswordCheck() { var pw = document.getElementById('password').value; if (pw.length < 5 || document.getElementById('password').value == '') { alert('Password must be 5-12 characters'); return false; } var pw = document.getElementById('password').value; if (pw.length > 12 || document.getElementById('password').value == '') { alert('Password must be 5-12 characters'); return false; } var pw = document.getElementById('username').value; if (pw.length < 6 || document.getElementById('username').value == '') { alert('Username must be 6-12 characters'); return false; } var pw = document.getElementById('username').value; if (pw.length > 12 || document.getElementById('username').value == '') { alert('Username must be 5-12 characters'); return false; } var regex=/^[0-9A-Za-z]+$/; //^[a-zA-z]+$/ if(regex.test(document.getElementById('password').value)){ return true; } else { alert("Password must contain only numbers and letters") return false; } var regex=/^[0-9A-Za-z]+$/; //^[a-zA-z]+$/ if(regex.test(document.getElementById('username').value)){ return true; } else { alert("Username must contain only numbers and letters") return false; } } </script>
October 11, 200817 yr Author I managed to solve the problem. All i did was simply add an !: if(!regex.test
Create an account or sign in to comment