0

i need to send the input fields "data-validation" to javascript function.

form is

 <input type="text" name="fname" id="fname"  data-validation="length" onchange="validate(this);" required />

Javascript is

 function validate(value)
    {
        var value=value.value;
        var validation=value.data-validation;
        alert(value +"  "+ validation);
        return false;
    }

i can't get the value of data-validation in javascript.

Crazy Developer
  • 339
  • 3
  • 18

3 Answers3

2

please review this code getAttribute() is used to get attribute of element

function validate(ele)
        {
            var value=ele.value;
            var validation=ele.getAttribute("data-validation")
            alert(value +"  "+ validation);
            return false;
        }

Demo here http://jsfiddle.net/GgfM3/

Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30
1

Attribute of an element cannot be accessed just through the dot. You should use "getAttribute("myAttribute")" in javascript and "attr("myAttribute")" with jQuery.

How about using jQuery? The post here suggests that "onchange is only triggered when the control is blurred". The jQuery .on can monitor value changes in the input.

$("#fname").on("input", function(){
    var value=$(this).val();
    var validation=$(this).attr("data-validation");
    alert(value +"  "+ validation);
}

Check example here.

Community
  • 1
  • 1
Carson Ip
  • 1,896
  • 17
  • 27
0

You can retrive the Attribute values using the getAttribute('value') method.

In you example you can get the value using

function validate(data)
    {           
       var dataFromTextbox = data.getAttribute("data-validation")
       alert(dataFromTextbox);            
    }
Ravindra Miyani
  • 360
  • 6
  • 14