-2

I have a dropdown and a textbox. I have populated dropdown with datatypes like number, character, float etc. Based on dropdown value need to validate textbox value

$("#textbox").on("keydown",function (e) { 
    var dropdown_val = $("#dropdown option:selected").val(); 
    if ((e.which < 48 || e.which > 57) && dropdown_val === $.trim('NUM')) {     
        $("#transac_errmsg" + transaction_hid).html("Digits only").show().fadeOut("slow"); 
        e.preventDefault(); 
    } 

    if ((e.which <= 64 && e.which >= 91) || (e.which <= 96 && e.which >= 123) && dropdown_val === $.trim('CHAR')) { 
        $("#transac_errmsg" + transaction_hid).html("Letters only").show().fadeOut("slow"); 
        e.preventDefault(); 
    } 
});

Only first part of if works for me and not other else part. Help me to do this.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
svl
  • 11
  • 1
  • 8
  • I haven't read the question yet, but there is a missing double quotes `"` after `keyup` – Abdo Adel Jun 17 '15 at 01:01
  • I believe he is writing a pseudocode. – Rey Libutan Jun 17 '15 at 01:02
  • Adding your HTML code and proper jQuery code would be helpful. – Armaiti Jun 17 '15 at 01:04
  • Actual Code : $("#textbox").on("keydown",function (e) { var dropdown_val=$("#dropdown option:selected").val(); if ((e.which < 48 || e.which > 57) && dropdown_val === $.trim('NUM')) { $("#transac_errmsg"+transaction_hid).html("Digits only").show().fadeOut("slow"); e.preventDefault(); } if ((e.which <= 64 && e.which >= 91) || (e.which <= 96 && e.which >= 123) && dropdown_val === $.trim('CHAR') ) { $("#transac_errmsg"+transaction_hid).html("Letters only").show().fadeOut("slow"); e.preventDefault(); } }); – svl Jun 17 '15 at 08:05

1 Answers1

0

Do something like this.

For the keycodes, visit here.

For the actual validation, see this good answer.

HTML

<select id="myDropdown">
    <option value="number">Number</option>
    <option value="text">Text</option>
    <option value="float">Float</option>
</select>
<input type="text" id="myInput"  value="" />

JS

$("#myInput").on("keydown", function(e) {
    var dropdown = $("#myDropdown").val();
    var inputValue = $(this).val();

    if(dropdown === "number") {
        //"Do your number validation here
    } else if(dropdown === "text") {
        //Do your text validation here
    } else  if(dropdown === "float") {
        //Do your float validation here
    }
});

jsFiddle Demo

Community
  • 1
  • 1
Rey Libutan
  • 5,226
  • 9
  • 42
  • 73