-1

instantly alert when user break validation, but here which script in 1 problem, when i click input textfield than alert msg even i didn't enter any value.

<script>
        function validation(){
            var rate = document.getElementById("rate").value;
            if (rate == "" || rate.length<5);
            alert("it is wrong value");
            return false;
        }
    </script>
<div class="form-group col-lg-5">
                {!! Form::label('rate', 'Traded Rate:') !!}
                {!! Form::text('rate', null, ['class'=>'form-control', 'onclick'=>'return validation()'])!!}
            </div>
  • your if statement has no "body" { }. So alert is called every time, even if the if is false. – physi Jun 18 '18 at 14:24
  • @physi In Javascript, brackets are not required for single-line if statements. See https://stackoverflow.com/questions/4797286/are-curly-braces-necessary-in-one-line-statements-in-javascript for more info. – Tim Lewis Jun 18 '18 at 14:36
  • @Tim Lewis: yes but then the trailing ";" is wrong, isn't it? – physi Jun 18 '18 at 14:42
  • @physi Ignoring my deleted comment, yes, semicolon does seem to be an issue here. – Tim Lewis Jun 18 '18 at 14:51
  • So what can i do for that? -@physi –  Jun 19 '18 at 04:04

1 Answers1

3

The validation appears to be triggered since you have set onclick on the input tag. Try replacing onclick into oninput so that the function will be called when you start to input something in the input field. Also, consider @physi's comment by modifying your function into the code below:

function validation(){
    var rate = document.getElementById("rate").value;
    if (rate == "" || rate.length<5){
      alert("it is wrong value");
    }
    return false;
}
undrftd
  • 331
  • 2
  • 10
  • I used your code, but its giving me a same result -@undrftd –  Jun 19 '18 at 04:06
  • Do you got it my question? when i feel textfield wrong value after giving me a msg, but it alert before feel value –  Jun 19 '18 at 04:09
  • Do you mean it will validate only when you try to input in the field? Did you already change onlick to oninput? – undrftd Jun 19 '18 at 04:58