how to include multiple if statement conditions in jQuery - javascript

I have an if statement that needs to look like this:
UPDATE
$("input#textbox").keypress(function(e){
key==e.which;
if($("input#textbox").length <=7 && (key===13 || $("div#search-button").click())){
/////SOME FUNCTION////
};
});
I'm trying to execute the "SOME FUNCTION" area only if the input length is <=7 and either the enter button is pressed or the "search" button is clicked.
Furthermore, I want to combine these 2 different function initiators so that they execute the same function but don't know how to do it:
$("input#textbox").keypress(function(e){
FUNCTION A
};
AND
$("div#search-button").click(function(){
FUNCTION A
};

EDIT:
This is what you have to do:
I am assuming that you want the text length and not number of textboxes.
You want to execute FunctionA when enter is pressed on textbox or search button is clicked:
$("input#textbox").keypress(function(e){
key==e.which;
if (key === 13) // if enter is pressed
{
if ("#textbox").val().length >= 7) //if textbox has more than 7 characters
{
functionA();
}
}
});
$("div#search-button").click(function(){ functionA();});
HTH

This is how I would do it:
$("#search-button").click(function(){
$("#textbox").keypress(function(e,clicked){
(clicked || e.which===13) && $(this).val().length < 8 && functionA();
}).trigger("keypress",[true]);
});
function functionA(){
alert("hey!");
}

Related

To open a new web page after pressing the enter key 3 times

I need the script for functioning the window.location only after pressing the ENTER key 3 or n times.
This is my current code and i am trying to modify it. Need help, still.
function KeyPress(f) {
var event = window.event? event : f
if (event.keyCode == 13)
window.location = './index.html';
}
document.onkeypress = KeyPress;
Following your instructions, after three press to the key ENTER, it should run the code that will call window.location. In this example, I'm using console.log to prove it is doing what you asking.
Note: When you run it, you need to click with the mouse where it says "Press Enter 3 times.". In this way, the browser will focus on that section. Then, you can press the ENTER key three times.
document.addEventListener("keyup", (e) => enterKeyPressed(e))
let counter = 1;
function enterKeyPressed(event) {
console.log("Key", event.keyCode, " Pressed:", counter);
if (event.keyCode == 13 && counter == 3) {
console.log("Enter key is pressed");
// window.location = "<url you want to go>";
return true;
}
counter++;
return false;
}
Press Enter 3 times.
Check the log.

Check php input on enter key

I am editing todo list plugin in wp , and i don't know how to make sure that empty task cannot be added on Enter key.
I have tried , but it does not work .
if(e.which == 0) {
echo '<script>alert("Cannot be empty")</script>;
}
Full section
// check pressed keys and then do action accordingly.
function check_key(e , ele) {
//detect enter
if(e.which == 13) {
add_new_note();
}
//detect backspace
if(e.which == 8) {
if(jQuery.trim(jQuery(ele).val()) == '') {
if(jQuery('.sm_at_textarea_div').length > 1 ) {
jQuery(ele).closest('.sm_at_textarea_div').prev().find('input').length ) ;
This line here is looking for to check if the key is null.
if(e.which == 0) {
echo '<script>alert("Cannot be empty")</script>;
}
This is not what you're looking for. You need to validate if the input element's value is null or empty.
I can't see all your script but in your add_new_note() function, you should validate the value and then alert the error message if needed.
Something like this:
function add_new_note() {
//get the value
var value = $('#myInputfield').val();
if (value === '') {
alert("Cannot be empty")
}
else {
//... do whatever the function already does.
}
}
By the way, you seem to be mixing PHP and jQuery without any logic to it. In the line below, you're already in javascript and you're echoing a new <script> tag. This will give you errors.
if(e.which == 0) {
echo '<script>alert("Cannot be empty")</script>;
}

JQuery selecting only the element inside the selected one

I'm currently having problem selecting the right element on a form
Here is my code:
$(document).ready(function(){
$(".wpcf7-submit").click(function () {
if($('.errorName').length > 0){
return false;
}
});
var selectorArray = [$('input[name="name"]'), $('input[name="Company"]')];
$.each(selectorArray, function(){
$(this).on('blur',function(){
if($('.errorName').length == 0 && (($(this).val().length < 4 && $(this).val().length != 0) || /^[a-zA-Z0-9- ]*$/.test($(this).val()) == false))
{
$(this).after('<span class="wpcf7-not-valid-tip errorName" role="alert">Field is not right.</span>');
}
else
{
if($(this).val().length > 3 && /^[a-zA-Z0-9- ]*$/.test($(this).val()) == true)
{
$(this).find('.errorName').remove();
}
}
});
});
});
Basically, I want to check, on unfocus, if what the user has input is good for a couple of fields inside the form, else an error message appears. Unfortunately, I can't seem to remove the error message, and this code stucks that message on the first field the user inputs wrong. After that, it won't come off even if the input typed after the first unfocus satisfies the conditions.
I'm testing on a simple form with 2 text inputs and 1 submit. Without the remove part, I can see both messages being displayed and on
$(this).find('errorName').first().remove();
It's the second element that gets stuck with the error message.
You're placing the errorName span after the input element, so you need to look for a sibling instead. You can use .next() for this.
$(this).next('.errorName').remove();

Alert and not allow if same word written twice in an input jquery or js

This is my input:
<input type='text' class='myprofiletags' name='my_profile_tags' value='' />
I want to alert and forbid the user if he/she writes the same word twice. I am entering values with commma e.g
games, animes, movies
jQuery('.myprofiletags').keypress(function(e)
{
if (jQuery(this).val().match(/,/g).length >= 10)
{
if(e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40)
{
}
else
{
e.preventDefault(); // stops the keypress occuring
alert('No more profile tags are allowed');
}
}
});
Set an event handler (onkeyup) on input box so that whenever user hits a key the handler gets called
In event handler take the values of input box and split them on comma
Check if any value is duplicate
If no return true
If yes show an alert
Synopsis:
$(".myprofiletags").keydown (function (e) {
alert ($(this).val());
});
You can also use change
$('input[name^="text"]').change(function() {
Edit: You are going in right direction, just split the value on comma and use each splitted value as an item of array.
See how to do that:
Split comma-separated input box values into array in jquery, and loop through it
Convert comma separated string to array

jQuery how to figure out if user inputed ' ' in text input area?

we have an input type="text" user prints into it some string. each time he inputs empty space char we want to call some function. How to do such thing with jQuery?
$(input).keydown(function (e) {
if ( e.which === 32 ) {
// do your thing
}
});
where input is a reference to your INPUT element.
Live demo: http://jsfiddle.net/uHpLg/
Check out jQuery's keypress event:
$("#YourInputId").keypress(function() {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 32) { // 32 = space keycode
//Do something
}
});
Here's a web page that has a list of javascript key codes.
Bind a listener to the input onchange event.
Compare the current value to the last value and check is the new character is a space
If so, call a function.
you can select the input text box and bind to it a change listener
but this will work only when the box is unfocused and the new value is different than the old value
$(document).ready(function () {
$('#mytextbox').change(function(){
if ($(this).val() == ' ')
{
// do whatever u want
}
});
});
but if u wanted something like autosuggestion (don't wait for user to unfocus the text input ) u can create ur own listener
is when the user focus the text box ,key press u check the pressed key and if it's is = " " do whatever u want
and when the user unfocus the input , it stops checking it
$('#mytextbox').keypress(function (e){
if (e.keycode == 32 )
{
// do your code here
}
});
for more info about keycodes : http://asquare.net/javascript/tests/KeyCode.html
on keypress Jquery function documentation : http://api.jquery.com/keypress/

Categories

Resources