0

We created a JavaScript function to validate user name, it is working fine, but if i try to use this function to another input validation at the same jsp page, it is showing only text, i included text color in same function but it is not working in second input validation. is it possible to get?, if yes, how can i get?, i am starting level in java script, pls give your answer if anything, thank you.

for example , this is my js function:

function name_validation()
      {
         if(true){
          var unam=document.getElementById("v2").innerHTML="valid";
          v2.style.color="green"; //this line is not exist in second input validation
                 }
         else{
            var unam=document.getElementById("v2").innerHTML="invalid";
            v2.style.color="red"; //this line is not exist in second input validation
              }
      }

this is my jsp code:

name:<input type="text" id="name" onchange="name_validation();"><a id="v2"></a>
Father name:<input type="text" id="name" onchange="name_validation();"><a id="v2"></a>
3
  • 5
    NB: Java is to Javascript as Car is to Carpet. Commented Dec 18, 2013 at 13:40
  • hint: pass the controlId to the function and then find the control and perform the validation Commented Dec 18, 2013 at 13:40
  • Perform your validation for elements on submit button click or on form submit event. Commented Dec 18, 2013 at 13:42

3 Answers 3

2

Try this way hope it helps :

Name:<input type="text" id="name" onchange="name_validation(this.id);">
Father name:<input type="text" id="father_name" onchange="name_validation(this.id);">

function name_validation(id){ 
  if(document.getElementById(id).value == ''){
    alert(id + 'is null');
  }

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

1 Comment

thank you, i can get alert message with text indicate, but i can't get color of text on second input validation for valid and invalid text. i updated my question again.could you see pls.
2

You can pass current object like this

name:<input type="text" id="name" onchange="name_validation(this);">
Father name:<input type="text" id="father_name" onchange="name_validation(this);">

then to do (JQuery):

function name_validation(obj) {
var value = $(obj).val();
...
}

for javascript

function name_validation(obj) {
var value = obj.value;
...
}

2 Comments

thank you alex, i can get alert message with text indicate, but i can't get color of text on second input validation for valid and invalid text. i updated my question again pls see.
It should be document.getElementById("v2").style.color="green"; and red as well.
0

Try following.

Name:<input type="text" id="name" class="validate">
Father name:<input type="text" id="father_name" class="validate">
$(function(){
   $('.validate').on('blur',function(){
      if($(this).val() == '')
      {
         $(this).css({'border-color':'red','background':'somecolor '});
      }
      else{
         $(this).css({'background':'somecolor'});
      }   

   });

});

Comments

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.