I have 52 members in my database. When displayed in the admin panel, each one has a form associated with it so I can make notes for each member.
Each Member has an ID so I name each form on the page like this: "frmRepNotes<%=MemberList("MemberID")%>" based on the memberID from the database. This results in something like frmRepNotes67453. I now want to validate the form before saving the notes.
How can I use that number in the JavaScript?
function validate(strid)
{
var ErrorFound = 0
if (document.'+strid+'.taNotes.value == '')
{
ErrorFound = ErrorFound + 1
}
if (ErrorFound == 0)
{
document.'+strid+'.submit();
}
}
How can this be done?
Consider refactoring to be something like this?
function validate(strid)
{
var ErrorFound = 0;
var theForm = document.getElementById("frmRepNotes"+strid);
if (theForm.taNotes.value == '')
{
ErrorFound +=1;
}
if (ErrorFound == 0)
{
theForm.submit();
}
}
Related
I have a script that calls a prompt when I push a button and gets a number from the user. The number is stored in a cookie. Then that script loads a view that gets the number from the cookie and passes it to a form.
Currently I have the cookie being saved; I can see it in the browser debugger. But when I try to get the cookie in my view it always returns null.
I think the request sends a cookie with a null value before updating the cookie.
JavaScript
$(document).ready(function() {
$(document).off('click', '.order_button').on('click', '.order_button', function() {
// $(".on_button").click(function() {
var amount = prompt("Please enter amount of orders" ,"0")
if (parseInt(amount) <= 0 /* || amount === null */) {
alert("Please enter integer greater than 0")
} else if (parseInt(amount) > 0) {
document.cookie = "amount=" + amount; //+ ";max-age=60";
console.log(document.cookie)
var link = $(this).find('a');
console.log(link.attr('href'));
window.location.href=link.attr('href');
console.log("OK");
}
return false;
});
});
View
def add_order(request, meal_slug, meal_restaurant):
print("OK")
amount = get_server_side_cookie(request, 'amount', '1')
print(amount)
//clear_amount_cookie(request)
if amount is not None:
meal = Meal.objects.get(
slug=meal_slug, restaurant_slug=meal_restaurant)
user = UserProfile.objects.get(user=request.user)
order = Order.objects.create(
meal=meal, customer=user, amount=amount, date=datetime.now())
order.save()
//clear_amount_cookie(request)
return redirect('food_cloud:index')
Get cookie function
def get_server_side_cookie(request, cookie, default_val=None):
val = request.COOKIES.get(cookie)
print(val) //This is where I see the null value
try:
int(val)
except (ValueError, TypeError):
val = default_val
print(val + " here")
return val
I solved the problem by implementing the jquery.cookies.js objects Link: https://github.com/carhartl/jquery-cookie
Then I replaced the code for creating the cookie. Apparently JQuery is not good for handling cookies directly
Changed code
$(document).ready(function() {
$(document).off('click', '.order_button').on('click', '.order_button', function() {
var amount = prompt("Please enter amount of orders" ,"0");
if (parseInt(amount) <= 0 /* || amount === null */) {
alert("Please enter integer greater than 0")
} else if (parseInt(amount) > 0) {
Cookies.set('amount', amount) //This is the new code for creating cookie
console.log(document.cookie)
var link = $(this).find('a');
console.log(link.attr('href'));
window.location.href=link.attr('href');
console.log("OK");
}
return false;
});
});
I am new to JS and i am trying to update option value using prompt. I have same ID element in different forms.
I have two forms and both having same IDs:dc_list_Z_side and dc_list_A_side.
Is there a way i can select ID in form using JS?
JS CODE:
function add_dc_form_down2() {
var theSelectA = form_down2.dc_list_A_side;
if (theSelectA[theSelectA.selectedIndex].value == "zzzzADD DC zzzzzz") {
var dc_name = prompt("Enter DC Name");
if (dc_name != null){
dc_a = document.getElementById("dc_list_A_side")
dc_a.options[dc_a.selectedIndex].text = dc_name;
dc_a.options[dc_a.selectedIndex].value = dc_name;
}
}
var theSelectZ = form_down2.dc_list_Z_side;
if (theSelectZ[theSelectZ.selectedIndex].value == "zzzzADD DC zzzzzz") {
var dc_name2 = prompt("Enter DC Name");
if (dc_name2 != null){
dc_z = document.getElementById("dc_list_Z_side");
dc_z.options[dc_z.selectedIndex].text = dc_name2;
dc_z.options[dc_z.selectedIndex].value = dc_name2;
}
}
return false;
}
function add_dc_form_flap() {
var theSelectA = flap_form.dc_list_A_side;
if (theSelectA[theSelectA.selectedIndex].value == "zzzzADD DC zzzzzz") {
var dc_name = prompt("Enter DC Name");
if (dc_name != null){
dc_a = document.getElementById("dc_list_A_side")
dc_a.options[dc_a.selectedIndex].text = dc_name;
dc_a.options[dc_a.selectedIndex].value = dc_name;
}
}
var theSelectZ = flap_form.dc_list_Z_side;
if (theSelectZ[theSelectZ.selectedIndex].value == "zzzzADD DC zzzzzz") {
var dc_name2 = prompt("Enter DC Name");
if (dc_name2 != null){
dc_z = document.getElementById("dc_list_Z_side");
dc_z.options[dc_z.selectedIndex].text = dc_name2;
dc_z.options[dc_z.selectedIndex].value = dc_name2;
}
}
return false;
}
Is there a way i can select ID in form using JS?
No.
If you are speaking about having the same form elements on the same page, using the same id (e.g. "dc_list_A_side") you cannot fetch them with document.getElementById(). The id-attribute has to have a unique value for the whole HTML page. JavaScript does not necessarily rely on valid HTML, but in this case it is simply impossible for JavaScript to achieve what you want.
Consider changing the IDs to "dc_list_A_side_a" and "dc_list_A_side_b" for example.
I am using an email validation script for my form. I am trying to add a function that returns TRUE if the email field contains certain spammy domains, like "mail.ru" and others... Here's what I have, that's not working. Any help would be appreciated!
Also, any way to expand this to add multiple values to search the email input?
"Email" is the field name in my form.
Thanks, Nick
function DoCustomValidation() {
var frm = document.forms["contactus"];
if (frm.Email.value.includes("mail.ru")) {
sfm_show_error_msg('Sorry, no spam allowed!');
return false;
} else {
return true;
}
}
It should be something like this
function DoCustomValidation() {
var frm = document.forms["contactus"];
var spams = ['mail.ru', 'something.ru']; //Keep spam sites in this array
var i, token = frm.Email.value, hasSpam = false;
for (i = 0; i < spams.length; i++) {
if (token.includes(spams[i])) {
hasSpam = true;
break;
}
}
if (hasSpam) {
sfm_show_error_msg('Sorry, no spam allowed!');
}
return hasSpam;
}
Can someone help me out with validation of two text-box with same email Id.
I was able to pop an alert if both the text-box contain the same email Id via JavaScript(my requirement was both text-box cant have same email) but now I m facing a problem if second text box contain more then one email_Id separated my comma(,) the validation doesn't work.
I don't want email that is present in first text box repeat into second text-box.
My code:
<script language="javascript" type="text/javascript">
function validated() {
if (document.getElementById("<%=txtCountry.ClientID %>").value = document.getElementById("<%=txtnewViewer.ClientID %>").value) {
alert("Presenter cant be attende");
return false;
}Else{
return true;
}
}
</script>
check this code out
<script language="javascript" type="text/javascript">
function validated()
{
if (document.getElementById("<%=textbox1.id %>").value == document.getElementById("<%=textbox2.id %>").value)
{
alert("text-box cant have same email");
return false;
}
else
{
alert("Valid");
return true;
}
}
</script>
Can you try this.
var f_email = document.getElementById("f_email").value;
var s_email= document.getElementById("s_email").value;
if(f_email === s_email) {
// do something when email ids are same.
alert("email ids are same");
}
else {
// do something when email ids are same.
alert("email ids are not same");
}
First, you if statement contains an = who always return true and modify your variable (in place of ==).
function validated() {
var clientId = document.getElementById("<%=txtCountry.ClientID %>").value,
viewerId = document.getElementById("<%=txtnewViewer.ClientID %>").value;
if (clientId == viewerId) {
alert("Presenter cant be attende");
return false;
}
return true;
}
After that you can use : Array.indexOf():
var clients = clientId.split(","), viewers = viewerId.split(",");
// Here we have two arrays with all datas
for(var i = 0; i < clients.length; i++){
var k = viewers.indexOf(clients[i]);
if(k !== -1) {
alert(clients[i], "=", viewers[k]);
}
}
I use pdf.js library to generate html5 page from pdf but some capabilities not working. I try to get value for input radio but still abortively:(
For example, in core.js script there are a few lines of code that take type of field:
var fieldType = getInheritableProperty(annotation, 'FT');
if (!isName(fieldType))
break;
item.fieldType = fieldType.name;
How I can get feild value?
I found the solution that work form me!
Add this code around line 260 of core.js file:
function setRadioButton(annotation, item) {
var ap = annotation.get('AP');
var nVal = ap.get('N');
var i = 0;
nVal.forEach(function(key, value){
i++;
if(i == 1 || i == 2) {
if(key != 'Off')
item.value = key;
}
});
}
And this code around line 370 of core.js file:
if (item.fieldType == 'Btn') {
if (item.flags & 32768) {
setRadioButton(annotation, item);
}
}
Also, if you want to get values from select input, you can use this code:
if(item.fieldType == 'Ch') {
item.value = annotation.get('Opt') || []; //return array of values
}