I need to perform validation on TextArea based on below scenario:
If Mobile is selected in the dropdown, only number should allow to enter in the TextArea.
If Email is selected in the dropdown, we can enter any character in the TextArea.
image snippet here
Below is my code to achieve above scenario. I have performed validation based on class name of Text Area. When I change dropdown value, I am changing the class name of Text Area.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function changeNotifyTypeValue(textboxControl)
{
textboxControl.value="";
if (textboxControl.className=="mobileValidation")
textboxControl.className="emailValidation";
else
textboxControl.className="mobileValidation";
}
$(function() {
$('.numberValidation').keyup(function() {
this.value = this.value.replace(/[^0-9,][.]?/g, '');
});
$('.emailValidation').keyup(function() {
//email validation
});
});
</script>
</head>
<body>
<table border="1" class="display"
id="NotificationTable">
<thead>
<tr style="background: #0086cd; color: #fff;">
<th>Update NotifyType</th>
<th>Update Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select
id="notifyTypeID0"
class="form-control" name="notifyType0" onchange="changeNotifyTypeValue(updateAddress0)" >
<option selected value=EMAIL>EMAIL</option>
<option value="mobile">Mobile</option>
</select>
</td>
<td>
<textarea name="address0" id="updateAddress0"
class="emailValidation">abc#gmail.com</textarea>
</td>
</tr>
<tr>
<td>
<select id="notifyTypeID1" class="form-control" name="notifyType1" onchange="changeNotifyTypeValue(updateAddress1)" >
<option value="EMAIL">EMAIL</option>
<option selected value="mobile">Mobile</option>
</select>
</td>
<td> <textarea name="address1" id="updateAddress1" class="numberValidation">9999999999</textarea> </td>
</tr>
</tbody>
</table>
</body>
</html>
Here is my doubt
I can see through inspect element, when I change dropdown value, the class name of text Area is being changed on run time. But, still validation is being perform based on old class name of text Area.
There are some issue with your <script>, you are trying to use pure javascript in jquery.
Once try this script and check
$(document).ready(function(){
$('textarea').keyup(function(){
if($(this).hasClass("mobileValidation")){
var cv = $(this).val();
$(this).val(cv.replace(/[^0-9,][.]?/g, ''));
} else if($(this).hasClass("emailValidation")){
//your email validation code;
}
});
});
The code is messed up with both javascript and jQuery. I'm providing javascript only solution. And classes are also mismatching (mobileValidation and numberValidation).
Here is the running code. You can run code snippet and check.
<html>
<head>
<script>
function changeNotifyTypeValue(textboxControl) {
textboxControl.value = '';
if (textboxControl.className == "mobileValidation")
textboxControl.className = "emailValidation";
else
textboxControl.className = "mobileValidation";
}
function validate(textboxControl) {
console.log(textboxControl.className);
if (textboxControl.className == "emailValidation") {
console.log('email');
} else {
console.log('number');
textboxControl.value = textboxControl.value.replace(/[^0-9,][.]?/g, '');
}
}
</script>
</head>
<body>
<table border="1" class="display" id="NotificationTable">
<thead>
<tr style="background: #0086cd; color: #fff;">
<th>Update NotifyType</th>
<th>Update Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="notifyTypeID0" class="form-control" name="notifyType0" onchange="changeNotifyTypeValue(updateAddress0)">
<option selected value=EMAIL>EMAIL</option>
<option value="mobile">Mobile</option>
</select>
</td>
<td>
<textarea name="address0" id="updateAddress0" class="emailValidation" onkeyup="validate(updateAddress0)">abc#gmail.com</textarea>
</td>
</tr>
<tr>
<td>
<select id=" notifyTypeID1" class="form-control" name="notifyType1" onchange="changeNotifyTypeValue(updateAddress1)">
<option value="EMAIL">EMAIL</option>
<option selected value="mobile">Mobile</option>
</select>
</td>
<td> <textarea name="address1" id="updateAddress1" class="mobileValidation" onkeyup="validate(updateAddress1)">9999999999</textarea> </td>
</tr>
</tbody>
</table>
</body>
</html>
Related
I am programming a quiz maker input form which is responsive to the users desired number of questions. These quizzes will be grouped by the students year and class, set up through a responsive menu read from a text file. This text file will be added to by the user by creating 'Topics' in the quizzes, and grouping them as such. I want to create a button at the bottom of the form which copies the input fields for 'question', 'answer1', 'answer2' and 'answer3'.
<html>
<head>
<title>Quiz Form</title>
<link rel=“stylesheet” href=“TRYstyle.css”>
</head>
<body>
<h3>Quiz form</h3>
<table>
<form onSubmit=“WriteToFile(this.txt)>
<tr>
<td>Year</td>
<td>
<select name="Year" id="schoolyear">
<option value="1">7</option>
<option value="2">8</option>
<option value="3">9</option>
<option value="4">10</option>
<option value="5">11</option>
<option value="6">12</option>
<option value="7">13</option>
</td>
</tr>
<tr>
<td>Class</td>
<td>
<select name=“group” id=“group”>
<option value="1">H</option>
<option value="2">E</option>
<option value="3">A</option>
<option value="4">R</option>
<option value="5">T</option>
</td>
</tr>
<tr>
<td>Topic</td>
<td><input type=“text” placeholder=“Topic” name=“topic” id=“topic” maxlength=“200”/></td>
</tr>
<tr>
<td>Title</td>
<td><input type=“text” placeholder=“Title” name=“title” id=“title” maxlength=“200”/></td>
</tr>
<tr>
<td>Question</td>
<td><input type=“text” placeholder=“Question” name=“question” id=“question” maxlength=“200”/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type=“text” placeholder=“Answer” name=“answer1” id=“answer1” maxlength=“200”/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type=“text” placeholder=“Answer” name=“answer2” id=“answer2” maxlength=“200”/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type=“text” placeholder=“Answer” name=“answer3” id=“answer3” maxlength=“200”/></td>
</tr>
<td colspan="2">
<input type="submit" value="Submit">
</td>
</form>
</table>
<script>
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile(“using theseee/this.txt", True);
var year = document.getElementById('year');
var group = document.getElementById('group');
var topic = document.getElementById('topic');
var title = document.getElementById('title');
var question = document.getElementById('question');
var option1 = document.getElementById('answer1');
var option2 = document.getElementById('answer2');
var option3 = document.getElementById('answer3');
s.writeline("Title: " + title);
s.writeline("Question: " + question);
s.writeline("Option1: " + answer1);
s.writeline(“Option2: " + answer2);
s.writeline("Option3: " + answer3);
s.writeline("-----------------------------");
s.Close();
}
</script>
</body>
</html>
Also, to store the various 'topics', would I need to create a text file dedicated to these considering the menu is responsive to this? Or can i read the first line, lets say, from each text file relevant and store them in the menu? What is the best way to go about doing this?
I am sorry if this seems very low level, but I am trying very hard to get the hang of all of this as I am new to it all.
I'm wanting to disable one select box when the other select box == 1 and visa versa. I know how to do this with IDs, but where this gets tricky is that the IDs and names of the select boxes that I'm trying to target are getting dynamically generated by php. So I need to be able to target the select elements without using the unique, numeric number inside of it. I've tried a few things before this such as the ":contains" or ":input" selector to narrow it down by tr but to no avail so I resulted to using wildcards but I'm still not able to get it working. I'm fairly new to jQuery so it's probably just a simple syntax error.
var update_tickets = function() {
if ($("select[id$='Online']").selectedIndex == "1") {
$("select[id$='On-site']").prop('disabled', true);
} else if ($("select[id$='On-site']").selectedIndex == "1") {
$("select[id$='Online']").prop('disabled', true);
} else {
$("select[id$='Online']").prop('disabled', false);
$("select[id$='On-site']").prop('disabled', false);
}
};
$(update_tickets);
$("select[id$='On-site']").change(update_tickets);
$("select[id$='Online']").change(update_tickets);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="em-tickets fusion-table table-1 table" cellspacing="0" cellpadding="0">
<tr>
<th class="em-bookings-ticket-table-type">Location</th>
<th class="em-bookings-ticket-table-price">Price</th>
<th class="em-bookings-ticket-table-spaces">Spaces</th>
</tr>
<tr class="em-ticket" id="em-ticket-14">
<td class="em-bookings-ticket-table-type">On-site Enrollment</td>
<td class="em-bookings-ticket-table-price">$50.00</td>
<td class="em-bookings-ticket-table-spaces boyyyyyyy">
<select name="em_tickets[14][spaces]" class="em-ticket-selection" id="em-ticket-spaces-14-On-site">
<option>0</option>
<option>1</option>
</select>
</td>
</tr>
<tr class="em-ticket" id="em-ticket-16">
<td class="em-bookings-ticket-table-type">Online registration</td>
<td class="em-bookings-ticket-table-price">$50.00</td>
<td class="em-bookings-ticket-table-spaces boyyyyyyy">
<select name="em_tickets[16][spaces]" class="em-ticket-selection" id="em-ticket-spaces-16-Online">
<option>0</option>
<option>1</option>
</select>
</td>
</tr>
</table>
You were trying to use selectedIndex to get the value of your select elements, but that's vanilla JS which works on DOM nodes, not jQuery selectors, and doesn't return a string (just the index). (The proper way to access that would be $('select')[0].selectedIndex.)
Instead, use .val() to return the string value of an input/select element using jQuery.
Here's a fixed demo:
var update_tickets = function() {
if ($("select[id$='Online']").val() == "1") {
$("select[id$='On-site']").prop('disabled', true);
} else if ($("select[id$='On-site']").val() == "1") {
$("select[id$='Online']").prop('disabled', true);
} else {
$("select[id$='Online']").prop('disabled', false);
$("select[id$='On-site']").prop('disabled', false);
}
};
$(update_tickets);
$("select[id$='On-site']").change(update_tickets);
$("select[id$='Online']").change(update_tickets);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="em-tickets fusion-table table-1 table" cellspacing="0" cellpadding="0">
<tr>
<th class="em-bookings-ticket-table-type">Location</th>
<th class="em-bookings-ticket-table-price">Price</th>
<th class="em-bookings-ticket-table-spaces">Spaces</th>
</tr>
<tr class="em-ticket" id="em-ticket-14">
<td class="em-bookings-ticket-table-type">On-site Enrollment</td>
<td class="em-bookings-ticket-table-price">$50.00</td>
<td class="em-bookings-ticket-table-spaces boyyyyyyy">
<select name="em_tickets[14][spaces]" class="em-ticket-selection" id="em-ticket-spaces-14-On-site">
<option>0</option>
<option>1</option>
</select>
</td>
</tr>
<tr class="em-ticket" id="em-ticket-16">
<td class="em-bookings-ticket-table-type">Online registration</td>
<td class="em-bookings-ticket-table-price">$50.00</td>
<td class="em-bookings-ticket-table-spaces boyyyyyyy">
<select name="em_tickets[16][spaces]" class="em-ticket-selection" id="em-ticket-spaces-16-Online">
<option>0</option>
<option>1</option>
</select>
</td>
</tr>
</table>
Try this:
$('body').on('change', 'select[id$="On-site"]', update_tickets);
$('body').on('change', 'select[id$="Online"]', update_tickets);
I am trying to create an change event so when a user selects an option from a dropdown, the corresponding row in the table is highlighted (via adding a class).
I have tried two things.. jQuery and javascript.
Here is my jQuery:
$("#phoneType").change(function() {
if (document.getElementById("optionSelect").value == 2) {
var selectedStatus = parseInt(document.getElementById("optionSelect").value);
$('.clickable-row').removeClass("cra-children-item-selected");
$(this).addClass('cra-children-item-selected');
}
});
Note that I am using javascript for the first two lines because I kept getting "val() is not a function" errors.
I tried using javascript to simply add/remove classes, but it doesn't seem to like my syntax...
document.getElementsByClassName("clickable-row").className = "clickable-row";
document.getElementById(selectedStatus).className = "clickable-row item-selected");
What I don't understand is why the jQuery isn't working... I have another function that works perfectly:
$(".clickable-row").click(function(event) {
console.log($(this).attr('id'));
$('.clickable-row').removeClass("item-selected");
$(this).addClass('item-selected');
});
Why is the code above (on click) working, but not my on change function?
Here is HTML:
<table class="table table-striped table-bordered">
<tr>
<th scope="col">Type</th>
<th scope="col">Number</th>
</tr>
<tr id="1" class="clickable-row">
<td>
Work
</td>
<td>
705-556-6677
</td>
</tr>
<tr id="6" class="clickable-row">
<td>
Cellular phone
</td>
<td>
613-444-7777
</td>
</tr>
</table>
<select name="phoneType" id="phoneType">
<option value="-1">Select</option>
<option value="3">Home</option>
<option value="1">Work</option>
<option value="6">Cellular phone</option>
</select>
Check this, it's actually working.
$("#phoneType").change(function() {
var val = $(this).val();
$("tr").removeClass("selected");
$("#tr" + val).addClass("selected");
})
.selected {border: solid 1px #000; background-color:blue;};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-striped table-bordered">
<tr>
<th scope="col">Type</th>
<th scope="col">Number</th>
</tr>
<tr id="tr1" class="clickable-row">
<td>
Work
</td>
<td>
705-556-6677
</td>
</tr>
<tr id="tr6" class="clickable-row">
<td>
Cellular phone
</td>
<td>
613-444-7777
</td>
</tr>
</table>
<select name="phoneType" id="phoneType">
<option value="-1">Select</option>
<option value="3">Home</option>
<option value="1">Work</option>
<option value="6">Cellular phone</option>
</select>
Based on the HTML you presented, it seems the conditional is never true:
if (document.getElementById("optionSelect").value == 2) {
and should be rewritten referring to the select element for an option value that actually exists
$("#phoneType").change(function() {
var t = $(this),
tVal = t.val();
if (tVal === '3') {
console.log(tVal);
}
});
I would highly recommend you don't try to use id="1" as your row IDs. Use HTML5 data attributes instead:
<tr class="clickable-row" data-id="6">
Then simple jQuery which will select the row:
$("#phoneType").change(function() {
var id = parseInt($(this).val());
var $row = $('.clickable-row[data-id="' + id + '"]');
$('.clickable-row').removeClass("cra-children-item-selected");
if (id > 0) {
$row.addClass('cra-children-item-selected');
}
});
I have a form in which I'm hiding and showing fields according to the dropdown selection. Initially, when the button is clicked, only the dropdown selection option is shown and the rest of the fields are hidden. Once the form is saved, the user can add another form. So when I click the button for the second time, the .hide()/.show() doesn't work. Instead the entire form is displayed. Here is my code:
$(document).ready(function () {
$("#button").click(function () {
alert("handler called");
$("#name").hide();
$("#selection").on('change', function () {
alert("handler called1");
if ($("#selection").val() == "day") {
$("#name").show();
}
});
});
});
My HTML is:
<div id = "days">
<table>
<tbody>
<tr>
<td>
<div class="selection" id="selection">
<table>
<tbody>
<tr>
<td><label>selection</label></td>
</tr>
<tr>
<td><select class="selection"></select></td>
</tr>
</tbody>
</table>
</div>
<div class="name" id="name">
<table>
<tbody>
<tr>
<td><label>Name</label></td>
<td><input type="text"</input></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
The
alert("Handler called");
is fired the second time but the .hide() doesnt work.
It truly is hard to understand your end result, but after analyzing what you are doing more, i think this is what you want:
HTML:
<select id="days_dropdown">
<option value='0'>select one</option>
<option value='night'>night</option>
<option value='day'>day</option>
<option value='afternoon'>afternoon</option>
</select>
<form id="myForm" style="display:none">
<label>Name</label>
<input type="text">
<button type="submit">Save</button>
</form>
JS:
$(document).ready(function() {
$("#days_dropdown").change(function() {
if ($(this).val() === "day") {
$("#myForm").show();
} else {
$("#myForm").hide();
}
});
$("#myForm").submit(function() {
//..your code to save the form info
$(this).hide();
$("#days_dropdown").val(0);
return false;
});
});
Let me know if this is the desired result, and ill explain in more detail your mistakes.
On
$("#selection").on('change', function () {
you select your div with id "selection", not the selection tag. And also you should get value of selected option, not the value of "select" tag
Need to modified some in JS code for proper work:
$(document).ready(function () {
$("#button").click(function () {
$("#name").hide();
$("select.selection").on('change', function () {
var sel = this;
if (sel.options[sel.selectedIndex].value == "day") {
$("#name").show();
}
});
});
});
Here is a FIDDLE
Its hard to understand specifically what you are going for, but is this what you are trying to do?
HTML
<button id='btn'>Click Me</button>
<div id="days">
<table>
<tbody>
<tr>
<td>
<div>
<table>
<tbody>
<tr>
<td><label>selection</label></td>
</tr>
<tr>
<td>
<select id="selection">
<option value='something'>1</option>
<option value='day'>2</option>
<option value='something'>3</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
<div class="name" id="name">
<table>
<tbody>
<tr>
<td><label>Name</label></td>
<td><input type="text"</input></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
JS:
$(document).ready(function() {
$("#btn").click(function() {
$("#name").hide();
$("#selection").on('change', function() {
if ($("#selection").val() == "day") {
$("#name").show();
}
});
});
});
The problem I initially saw was your .on('change') , you were trying to attach this to a div, but you want to attach it to the select element
I am both adding and removing table rows with jQuery. I can add rows easily, but am having trouble removing ones that were created.
You can view the page in action here: http://freshbaby.com/v20/wic/request_quote.cfm, with the relevant code pasted below.
HTML
<table style="width:600px;" id="product-list" summary="Lists details about products users wish to purchase">
<thead valign="top" align="left">
<tr>
<th>Products</th>
<th>Language</th>
<th>Quantity</th>
<th></th>
</tr>
</thead>
<tbody valign="top" align="left">
<tr>
<td>
<cfselect query="getProductListing" name="product" size="1" display="name" value="name" queryPosition="below">
<option value=""></option>
</cfselect>
</td>
<td>
<select name="language" size="1">
<option value="English">English</option>
<option value="Spanish">Spanish</option>
</select>
</td>
<td>
<cfinput name="quantity" required="yes" message="Enter your desired quantity" size="10" maxlength="3" mask="999">
</td>
<td valign="bottom">Add Another Product</td>
</tr>
</tbody>
</table>
JavaScript:
<script>
$(function() {
var i = 1;
$(".addrow").click(function() {
$("table#product-list tbody > tr:first").clone().find("input").each(function() {
$(this).attr({
'id': function(_, id) { return id + i },
'value': ''
});
}).end().find("a.addrow").removeClass('addrow').addClass('removerow').text('< Remove This Product')
.end().appendTo("table#product-list tbody");
i++;
return false;
});
$("a.removerow").click(function() {
//This should traverse up to the parent TR
$(this).parent().parent().remove();
return false;
});
});
</script>
When I click the link to remove the row that said link is contained in, nothing happens. No script error, so it has to be logic.
Try this instead
$("#product-list").on('click','a.removerow',function(e) {
e.preventDefault();
//This should traverse up to the parent TR
$(this).closest('tr').remove();
return false;
});
This will ensure that newly created elements can be removed. When you use the $("a.removerow").click(.. it only affects the elements in existence (none) and not the ones that will be dynamically created.