1

So I have this code where I am saying if website_name==="" display error however it doesn't seem to work because if I type something and delete it so it's empty it still shows it's valid so it runs else statement, however if I type a number then I get an error, but I need it to work for empty values too.

var message;
    var inputField = document.getElementById("website_name");
$( "#website_name" ).keyup(function() {
  if(website_name==="" || !/^[a-z]*$/.test(this.value))
  {
    $("#website_name").css('border', '3px solid red');
    inputField.style.backgroundColor = "#f27676";
    message = 'Only lower case characters allowed!';
    $('div.modal-body .msgPlaceholder').empty().append(message);
  }else{
    $("#website_name").css('border', '3px solid green');
    inputField.style.backgroundColor = "#56c145";
    message = 'Your name is correct';
    $('div.modal-body .msgPlaceholder').empty().append(message);
  }
});
2
  • if(website_name=="" || !/^[a-z]*$/.test(this.value)) Commented Mar 8, 2017 at 12:07
  • The the "===" makes sure, that the data type is exactly the same as what you are comparing with. Assume website_name is undefined, then it is not true if you compare to an empty string. So use just two "==" and automatic typecasting takes place and will return a true as undefined matches an empty string. Commented Mar 8, 2017 at 12:10

1 Answer 1

6

You have to use this.value == "" and not website_name == "" since website_name is not a variable

if you want to use toLowerCase before checking, try this:

var val = this.value.toLowerCase();
if (val == "")

var message;
var inputField = document.getElementById("website_name");
$("#website_name").keyup(function() {
  if (this.value == "" || !/^[a-z]*$/.test(this.value)) {
    $("#website_name").css('border', '3px solid red');
    inputField.style.backgroundColor = "#f27676";
    message = 'Only lower case characters allowed!';
    $('div.modal-body .msgPlaceholder').empty().append(message);
  } else {
    $("#website_name").css('border', '3px solid green');
    inputField.style.backgroundColor = "#56c145";
    message = 'Your name is correct';
    $('div.modal-body .msgPlaceholder').empty().append(message);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="website_name" />

Sign up to request clarification or add additional context in comments.

4 Comments

I'd add that to make things fair and more universal. Don't use a regex to check for uppercase/lowercase... Websites aren't limited to letter from a to z. And a better idea would be to lowercase what the user input instead of checking for lowercase... just use "toLowerCase".
@LoïcFaure-Lacroix is that a coment to me or to Przemek?
@LoïcFaure-Lacroix so how would code look like with your suggestion?
@Przemek Since LoicFaure dont seem to reply, then i updated the answer, just above the snippet i added a toLowerCase way for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.