apellegrino Posted August 9, 2014 Share Posted August 9, 2014 In my class, I need to develop a simple JavaScript form for the user to input alphabetic characters, and than check to see if both Uppercase and Lowercase characters are used. This is what I have so far for the code, but I'm completely lost on why it's not working the way I need it to. Below are the exact instructions, I'm not looking for you to complete my code, just give me a few hints on where to go with this. Write a JavaScript program that ensures that user input contains only upper or lower case letters by using the String object and the RegEx object (regular expression). Construct a regular expression using the RegEx object that checks for upper or lower case letters only. Add the JavaScript statements. Add the JavaScript statement to accept text input from the user. Add the JavaScript statement, and use either the search() or match() methods to check the input string and then write the answer to the screen. <script type="text/JavaScript">function chkPwd() {var pwd = document.form1.pwd;var pwdMsg = document.getElementById('pwdMsg'); regex = /[A-Z]/;var pwd1 = pwd.value;if(pwd1.search(regex -- -1)) {pwdMsg.innerHTML = "Must contain at least one uppercase character"pwd.select();return;}else{ pwdMsg.innerHTML = "";}} </script></head> <body><form name="form1" action="" method="post"> <p> Password: <br><input type="text" name="pwd" onchange="chkPwd()" /><span id="pwdMsg"></span></p><p><input type="button" value="chkPass" onclick="chkPwd()"></p></form><div id="results"></div></body></html> Thanks! Link to comment Share on other sites More sharing options...
niche Posted August 9, 2014 Share Posted August 9, 2014 What's your first question? Link to comment Share on other sites More sharing options...
apellegrino Posted August 9, 2014 Author Share Posted August 9, 2014 Well to be honest...I'm a little confused. The input box shows up, and if I insert a "A" I still get the "Must contain at least one uppercase character". How do I fix this? I have used the browser consoles (chrome and firefox) to check for errors, but I am not getting any. Link to comment Share on other sites More sharing options...
Ingolme Posted August 10, 2014 Share Posted August 10, 2014 I think you messed up the syntax on this line: if(pwd1.search(regex -- -1)) { It's not giving an error because it's actually valid. Link to comment Share on other sites More sharing options...
apellegrino Posted August 10, 2014 Author Share Posted August 10, 2014 Yes, I actually removed this line, because it's not needed. But I'm still receiving the "Must contain an uppercase character" message when an uppercase letter has been inputted. Any idea on how to fix that. Here is my code again with the code line removed. <script type="text/JavaScript">function chkPwd() {var pwd = document.form1.pwd;var pwdMsg = document.getElementById('pwdMsg'); regex = /[A-Z]/;var pwd1 = pwd.value;pwdMsg.innerHTML = "Must contain at least one uppercase character"pwd.select();return;} </script></head> <body><form name="form1" action="" method="post"><p> Password: <br><input type="text" name="pwd" onchange="chkPwd()" /><span id="pwdMsg"></span></p><p><input type="button" value="chkPass" onclick="chkPwd()"></p></form><div id="results"></div></body></html> Link to comment Share on other sites More sharing options...
JamesB Posted August 10, 2014 Share Posted August 10, 2014 Well you do need to use either the search or match method. http://www.w3schools.com/jsref/jsref_search.asp http://www.w3schools.com/jsref/jsref_match.asp http://www.w3schools.com/jsref/jsref_obj_regexp.asp Link to comment Share on other sites More sharing options...
Ingolme Posted August 10, 2014 Share Posted August 10, 2014 In your most recent code you're not telling it to do anything. function chkPwd() { // Get the form element var pwd = document.form1.pwd; // Get a reference to the message element var pwdMsg = document.getElementById('pwdMsg'); // Create a RegExp object regex = /[A-Z]/; // Get the value of the form element var pwd1 = pwd.value; // Tell people the password is wrong pwdMsg.innerHTML = "Must contain at least one uppercase character" // What does "select()" do? pwd.select(); // End of function return;} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now