Common JS validation snippets

/* Comments, or at least single-line comments, aren't Codepad's strong area */ /* On keydown/keyup replace all non-numeric values for an empty space */ $('input[type="text"]').on('keydown keyup', function() { this.value = this.value.replace(/[^0-9\.]/g,''); }); /* Check if a text input is empty */ if ($('input[type="text"]').val() == '') { /* code to execute if text input indeed is empty */ } /* Check if there's no option selected in a select dropdown */ if ($('select option:selected').text() == '') { /* code to execute if there's no option selected */ } /* Get value of a selected option in a select dropdown */ $('select option:selected').val(); /* Select a determined option of a select dropdown dinamically */ $('input option[value="xxxxx"]').prop('selected', true);

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.