0

well guys, i have this function that checks for the value of two text-boxes and depending on whether they are equal or not, does specific things, here is the function:

function ajx()
{
    var e_value = document.getElementById('email').value;
    var text_value = document.getElementById('text2').value;
    try{
    if (e_value == text_value)
    {
        document.getElementById('email').style.border = '2px solid red';
        document.getElementById('mes').innerHTML = 'email has been taken';
    }
else if (e_value == "" || e_value == " "|| e_value.includes('@') == false||  e_value.includes('.') == false)
    {
        document.getElementById('email').style.border = '2px solid none';
        document.getElementById('mes').innerHTML = '';
    }
    else{
        document.getElementById('email').style.border = '2px solid blue';
        document.getElementById('mes').innerHTML = '';
    }
}

catch(e)
{
    document.write(e);
}
}

the main problem i am having here is that this code:

document.getElementById('email').style.border = '2px solid none';

only works when i click outside the text-box, is it possible to make it work whiles the cursor is inside the text-box(on-focus), and how can i archive it? thanks !!!

this is the html:

<div class="wrap-input100 validate-input" data-validate = "Valid email is required: [email protected]">
    <input class="input100 climate" type="email" name="email" placeholder="Email" id="email" onkeydown="ajx()" onmousemove="ajx();" onmouseover="ajx();" oninput="ajx();">

<label id="mes" style="color: red;" ></label>
                        </div>

2 Answers 2

2

only works when i click outside the text-box, is it possible to make it work whiles the cursor is inside the text-box(on-focus)

Seems like you are calling the function onblur (focus out). To me you can use oninput event which will fire on every input instead of focus:

Demo:

function ajx(){
  var e_value = document.getElementById('email').value.trim();
  var text_value = document.getElementById('text2').value.trim();
  try{
    if (e_value == text_value){
      document.getElementById('email').style.border = '2px solid red';
      document.getElementById('mes').innerHTML = 'email has been taken';
    }
    else if (e_value == "" || !e_value.includes('@') || !e_value.includes('.'))
    {
      document.getElementById('email').style.border = '2px solid none';
      document.getElementById('mes').innerHTML = '';
    }
    else{
      document.getElementById('email').style.border = '2px solid blue';
      document.getElementById('mes').innerHTML = '';
    }
  }
  catch(e){
    document.write(e);
  }
}
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: [email protected]">
  <input class="input100 climate" type="email" name="email" placeholder="Email" id="email" oninput="ajx();">
  <input id="text2"/> <br>
  <label id="mes" style="color: red;" ></label>
</div>

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

6 Comments

@CurtisCrentsil, please update the question with relevant HTML:)
@CurtisCrentsil, there is no element with id="text2" and id="mes"!!!
@CurtisCrentsil, also you do not need any of onkeydown="ajx()" onmousemove="ajx();" onmouseover="ajx();"
there are elements with text2 and mes, text 2 is generated by php and mess is the label name, and may i ask how do u guys put code in your comments?
@CurtisCrentsil, comments are not the right place to post code, though I have updated the answer with demo solution:)
|
0

Use the "onfocus" event to achieve it:

document.getElementById("email").onfocus = function() {
  myFunction();
}

function myFunction() {
  document.getElementById("email").style.border = '2px solid red';
}

3 Comments

so for that //some code... how would i change the border color?
i added document.getElementById("email").onfocus = function() { style.border = '2px solid red'; }; but now i can't type in the text box again
Take a look to the edited answer. It works just fine!

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.