Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Form Checker Javascript Problem

Featured Replies

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>

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.