I am facing this issue here.After i click alert in if statement the page should not reload.I have tried everything here
return false;
event.preventDefault();
3.window.stop();//This is working but for other conditions where page should reload it is not getting loaded.
4.return;
So as you can see in if statement when sel.options[sel.selectedIndex].text!="Meeting Room Name the alert is showed after i click ok the page should not reload.I have tried each and every solution and referred many posts.
$("input[id$='diidIOSaveItem']").click(function(ev)
{
var startDate = $("input[id$='DateTimeField_DateTimeFieldDate']")[0].value;
var EndDate = $("input[id$='DateTimeField_DateTimeFieldDate']")[1].value;
var startH = $("select[id$='DateTimeFieldDateHours']")[0].value;
var endH = $("select[id$='DateTimeFieldDateHours']")[1].value;
var startM = $("select[id$='DateTimeFieldDateMinutes']")[0].value;
var endM = $("select[id$='DateTimeFieldDateMinutes']")[1].value;
var meetingTitle = $("input[id$='TextField']").val();
if(meetingTitle == "" || meetingTitle == "undefined")
{
alert("You have not entered a Meeting Title.");
$("input[id$='TextField']").focus();
}
if(startDate != EndDate)
{
alert("Each Meeting must start and finish on the same day.");
$("input[id$='DateTimeField_DateTimeFieldDate']").focus();
}
if(startH == endH && startM == endM)
{
alert("End Hour/Minutes and Start Hour/Minutes can not be same.");
$("select[id$='DateTimeFieldDateHours']").focus();
}
if(startH > endH )
{
alert("End Hour must be greator than or equal to Start Hour.");
$("select[id$='DateTimeFieldDateHours']").focus();
}
if(sel.options[sel.selectedIndex].text =="Meeting Room Name") //if no meeeting room or more then one meeting room is selected.
{
alert("Please select One Meeting Room");
//$("input[id$='DateTimeField_DateTimeFieldDate']").focus();
// ev.preventDefault();
return false;
}
if(sel.options[sel.selectedIndex].text!="Meeting Room Name")
{
//if without any room selection clicking on save.
if(selectedValue.length == 1)
{
if(selectedValue.options[selectedValue.selectedIndex] != "undefined")
{
selectedValueres= selectedValue.options[selectedValue.selectedIndex].text;
//sel.options[sel.selectedIndex].text= selectedValueres ;
if((selectedValueres != "")&&(selectedValue.length == 1))
{console.log("selectedValueres "+ selectedValueres);
if (!PreSaveItem())return false;
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl40$g_2543e208_7ccd_45b5_9c3c_703dd452cee7$savebutton2$ctl00$diidIOSaveItem", "", true, "", "", false, true));
}
else //if no meeeting room or more then one meeting room is selected.
{
//alert("Please refresh page and click on new booking your booking is conflict with other user.");
GipAddSelectedItemsModified(ctl00_ctl40_g_2543e208_7ccd_45b5_9c3c_703dd452cee7_ff41_ctl00_ctl00_MultiLookupPicker_m);
selectedValueres= $("tr.tobehide select[id$='SelectResult']").find(":selected").text();
$("<option value='new value' selected='selected'>"+selectedValueres+"</option>").prependTo($("tr.tobehide select[id$='SelectCandidate']"));
}
//sel.options[sel.selectedIndex].text= selectedValueres ;
}
else //if no meeeting room or more then one meeting room is selected.
{
alert("Please select One Meeting Room");
return;
}
}
}
return false;
});
Related
I'm trying to teach myself how to code a simple text game. I found a YouTube tutorial and am working my way through Der Kerker. A problem I have is that his final code doesn't match what he did in the videos, so I'm stuck.
Here's my problem:
When you load the game, the commands "take sword" and "help" both work as designed. However, if you put in jibberish or an unrecognized command, the game is supposed to say, "I don't understand ... "
Right now, it only clears the input box but doesn't actually run the command.
Here's my fiddle of the game:
https://jsfiddle.net/megler/hv7hqp1e/
If I remove the (check == false) portion - then the "I don't understand" part will work. However, if you type "help" - it will run the help segment AND say "I don't understand help."
The goal is for it to only say "I don't understand" if the player types in an unrecognized command.
Here's the JS:
//Check To See If True Function
var check = false;
// Been To Room Variables
var beenToCorridor = true;
//
//Inventory
var sword = false;
//
//Current Room
var currentRoom = "nCorridor";
$(document).ready(function() {
$("form").submit(function() {
var input = $("#commandLine").val();
function check() {
check = true;
}
if (input == "help") {
$("#messageHelp")
.clone()
.insertBefore("#placeholder")
.fadeIn(1000);
check();
}
if (input == "take sword" && currentRoom == "nCorridor") {
$("<p>You picked up a sword.</p>")
.hide()
.insertBefore("#placeholder")
.fadeIn(1000);
check();
}
else if (input == "take sword" && currentRoom != "nCorridor") {
$("<p>The sword is not here.</p>")
.hide()
.insertBefore("#placeholder")
.fadeIn(1000);
check();
}
else if (check == false) {
$("<p>I don't understand " + input + ".</p>")
.hide()
.insertBefore("#placeholder")
.fadeIn(1000);
}
$("#commandLine").val("");
});
});
Hope that makes sense.
I think this is what you want:
Code Replaced:
else if (input != "take sword" && input != "help") {
$("<p>I don't understand " + input + ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
Snippet:
//Check To See If True Function
var check = false;
// Been To Room Variables
var beenToCorridor = true;
//
//Inventory
var sword = false;
//
//Current Room
var currentRoom = "nCorridor";
$(document).ready(function() {
$("form").submit(function() {
var input = $("#commandLine").val();
function check() {
check = true;
}
if (input == "help") {
$("#messageHelp").clone().insertBefore("#placeholder").fadeIn(1000);
check();
}
if (input == "take sword" && currentRoom == "nCorridor") {
$("<p>You picked up a sword.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
check();
}
else if (input == "take sword" && currentRoom != "nCorridor") {
$("<p>The sword is not here.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
check();
}
else if (input != "take sword" && input != "help") {
$("<p>I don't understand " + input + ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
else
{
return false;
}
$("#commandLine").val("");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="console">
<p id="message_startGame">Welcome to my game!</p>
<p id="area_northCorridor">You are in the North Corridor. There is a sword on the ground.</p>
<p id="messageHelp" style = "display: none;">Here is a list of commands</p>
<!-- PLACEHOLDER: THIS IS WHERE EVERYTHING WILL BE INSERTED BEFORE
--->
<div id="placeholder"></div>
<form onsubmit="return false;">
<input type = "text" size ="50" autofocus="autofocus" id="commandLine">
</form>
</div>
</body>
Use different name for check variable. You should also separate last else if.
//Check To See If True Function
var _check = false;
// Been To Room Variables
var beenToCorridor = true;
//
//Inventory
var sword = false;
//
//Current Room
var currentRoom = "nCorridor";
$(document).ready(function() {
$("form").submit(function() {
var input = $("#commandLine").val();
function check() {
_check = true;
}
if (input == "help") {
$("#messageHelp")
.clone()
.insertBefore("#placeholder")
.fadeIn(1000);
check();
}
if (input == "take sword" && currentRoom == "nCorridor") {
$("<p>You picked up a sword.</p>")
.hide()
.insertBefore("#placeholder")
.fadeIn(1000);
check();
}
else if (input == "take sword" && currentRoom != "nCorridor") {
$("<p>The sword is not here.</p>")
.hide()
.insertBefore("#placeholder")
.fadeIn(1000);
check();
}
if (_check == false) {
$("<p>I don't understand " + input + ".</p>")
.hide()
.insertBefore("#placeholder")
.fadeIn(1000);
}
$("#commandLine").val("");
});
});
You should
first initialize var check = false; somewhere, otherwise the if condition never passes
add an else to your list of if ... else if ... checks.
Here's is the corrected code:
$(document).ready(function () {
$("form").submit(function () {
var input = $("#commandLine").val();
// INITIALIZE CHECK VARIABLE HERE
var check = false;
function check() {
check = true;
}
if (input == "help") {
$("#messageHelp").clone().insertBefore("#placeholder").fadeIn(1000);
check();
}
// NEEDS AN ADDITIONAL ELSE HERE TO PREVENT DOUBLE MESSAGE AFTER HELP
else if (input == "take sword" && currentRoom == "nCorridor") {
$("<p>You picked up a sword.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
check();
} else if (input == "take sword" && currentRoom != "nCorridor") {
$("<p>The sword is not here.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
check();
} else if (check == false) {
$("<p>I don't understand " + input + ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
$("#commandLine").val("");
});
});
Reset the check variable back to false every time the form is submitted, so you s=tart from a clean slate each time it is called. Also rename your function 'check' to 'setChecked' to not cause confusion between the global variable and the local function name.
$("form").submit(function() {
check = false;
function setChecked() {
check = true;
}
if (input == "help") {
setChecked();
}
//etc...
}
It's because you redefine 'check' as a function, so it's not equal to false anymore.
The solution is to use another name for your boolean, for example 'ischeck', like this:
//Check To See If True Function
var ischeck = false;
// Been To Room Variables
var beenToCorridor = true;
//
//Inventory
var sword = false;
//
//Current Room
var currentRoom = "nCorridor";
$(document).ready(function() {
$("form").submit(function() {
var input = $("#commandLine").val();
function check() {
ischeck = true;
}
if (input == "help") {
$("#messageHelp").clone().insertBefore ("#placeholder").fadeIn(1000);
check();
}
if (input == "take sword" && currentRoom == "nCorridor") {
$("<p>You picked up a sword.</p>").hide().insertBefore ("#placeholder").fadeIn(1000);
check();
}
else if (input == "take sword" && currentRoom != "nCorridor") {
$("<p>The sword is not here.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
check();
}
else if (ischeck == false) {
$("<p>I don't understand " + input + ".</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
$("#commandLine").val("");
});
});
I have a two text box and Enable button.If the data is entered wrongly in the text box on clicking enable button it alerts it.If all the data is correct, on clicking the enable button,text must become disable .The problem is once i click enable it becomes disable for a second then the page loads it again goes to Enable.Can any one tell me what i am doing wrongly .Please provide a example if possible .Thanks in advance
Script to validate and change the button text
function validate
{
var cal1 = document.getElementById('<%=txtEndDate.ClientId%>').value;
var cal2 = document.getElementById('<%=txtStartDate.ClientId%>').value;
var button=document.getElementById('<%=button1.ClientId %>')
if (cal1 == '' && cal2 == '') {
alert("Start Date and End Date can not be left blank ");
return false;
}
if (cal1 == '') {
alert("End Date can not be left blank ");
return false;
}
if (cal2 == '') {
alert("Start Date can not be left blank ");
return false;
}
if (cal1 != '' || cal2 != '')
{
var dt1 = Date.parse(cal1);
var dt2 = Date.parse(cal2);
if (dt1 <= dt2) {
alert("End Date must occur after the Start date ");
return false;
}
}
//if the all the validation are correct it comes to this
if (button.value == "Enable")
button.value = "Disable";
else
button.value = "Enable";
return true;
}
button
<asp:Button ID="button1" OnClientClick=" return validate()" runat="server" Text="Enable" />
Condition will be && in place of || and you pass return false to stop postback
After completing your validation try to return false and use if - else will save your time of validate
function validate()
{
var cal1 = document.getElementById('<%=txtEndDate.ClientId%>').value;
var cal2 = document.getElementById('<%=txtStartDate.ClientId%>').value;
var button=document.getElementById('<%=button1.ClientId %>')
if (cal1 == '') {
alert("End Date can not be left blank ");
return false;
}
else if (cal2 == '') {
alert("Start Date can not be left blank ");
return false;
}
else if (cal1 != '' && cal2 != '')
{
var dt1 = Date.parse(cal1);
var dt2 = Date.parse(cal2);
if (dt1 <= dt2) {
alert("End Date must occur after the Start date ");
return false;
}
else{
//if the all the validation are correct it comes to this
if (button.value == "Enable")
button.value = "Disable";
else
button.value = "Enable";
}
}
return false;
}
and Button - <asp:Button ID="button1" OnClientClick=" return validate();" runat="server" Text="Enable" />
Your problem is that the default action of a click event of an button (<input type="button">) is to submit the form. You can prevent that by returning false from the event handler. the simplest way to do that in your case would be to ALWAYS return false from your validate function.
After completing your validation try to return false
function validate()
{
var cal1 = document.getElementById('<%=txtEndDate.ClientId%>').value;
var cal2 = document.getElementById('<%=txtStartDate.ClientId%>').value;
var button=document.getElementById('<%=button1.ClientId %>')
if (cal1 == '') {
alert("End Date can not be left blank ");
return false;
}
if (cal2 == '') {
alert("Start Date can not be left blank ");
return false;
}
if (cal1 != '' || cal2 != '')
{
var dt1 = Date.parse(cal1);
var dt2 = Date.parse(cal2);
if (dt1 <= dt2) {
alert("End Date must occur after the Start date ");
return false;
}
}
//if the all the validation are correct it comes to this
if (button.value == "Enable")
button.value = "Disable";
else
button.value = "Enable";
return false;
}
I have written a client-side form validation script for a registration form that works great in Google Chrome but won't validate in Firefox or Internet Explorer??
Here is the entire script:
<!--- Registration Form Validation --->
<script type="text/javascript">
//Validate radio buttons
function checkRadioArray(radioButtons){
for (var k=0; k < radioButtons.length; k++) {
if (radioButtons[k].checked) {
return true;
}
}
return false;
}
//Initialize error messages
function reportErrors(errors) {
var msg = "There were some problems with your form submission. Please correct the errors listed below \n";
var numError;
for (var j=0; j<errors.length; j++) {
numError = j +1;
msg += "\n" + numError + ". " + errors[j];
}
alert(msg);
}
//Validate text fields
function checkLength(text, min, max){
min = min || 1;
max = max || 10000;
if (text.length < min || text.length > max) {
return false;
}
return true;
}
//Validate select menus
function checkSelect(select){
return (select.selectedIndex > 0);
}
//Validate Registration Form
function validate(form){
//Set Variables
var errors = [];
var roommate = form.Roommate.value;
var room = form.Room.value;
var SatSess = form.SatSess.value;
var SunSess = form.SunSess.value;
if ( !checkRadioArray(form.RegType) ) {
errors[errors.length] = "Please select your registration type."
}
if ( !checkRadioArray(form.FirstTimer) ) {
errors[errors.length] = "Please indicate if this is your first time attending this conference.";
}
if ( !checkRadioArray(form.Tshirt) ) {
errors[errors.length] = "Please indicate your Tshirt size.";
}
if ( !checkRadioArray(form.Room) ) {
errors[errors.length] = "Please indicate your room choice.";
}
if (room == "Double") {
if ( !checkLength(roommate) ) {
errors.push("You selected a double room, please indicate a roommate.");
}
}
if (SatSess != "off") {
if( !checkSelect(form.Sess1_1) ) {
errors[errors.length] = "Please indicate your first choice for Session 1.";
}
}
if (SatSess != "off") {
if ( !checkSelect(form.Sess1_2) ) {
errors[errors.length] = "Please indicate your second choice for Session 1.";
}
}
if (SatSess != "off") {
if( !checkSelect(form.Sess2_1) ) {
errors[errors.length] = "Please indicate your first choice for Session 2.";
}
}
if (SatSess != "off") {
if ( !checkSelect(form.Sess2_2) ) {
errors[errors.length] = "Please indicate your second choice for Session 2.";
}
}
if (SatSess != "off") {
if( !checkSelect(form.Sess3_1) ) {
errors[errors.length] = "Please indicate your first choice for Session 3.";
}
}
if (SatSess != "off") {
if ( !checkSelect(form.Sess3_2) ) {
errors[errors.length] = "Please indicate your second choice for Session 3.";
}
}
if (SatSess != "off") {
if( !checkSelect(form.Sess4_1) ) {
errors[errors.length] = "Please indicate your first choice for Session 4.";
}
}
if (SatSess != "off") {
if ( !checkSelect(form.Sess4_2) ) {
errors[errors.length] = "Please indicate your second choice for Session 4.";
}
}
if (SunSess != "off") {
if ( !checkSelect(form.Sess5_1) ) {
errors[errors.length] = "Please indicate your first choice for Session 5.";
}
}
if (SunSess != "off") {
if ( !checkSelect(form.Sess5_2) ) {
errors[errors.length] = "Please indicate your second choice for Session 5.";
}
}
if (SunSess != "off") {
if ( !checkSelect(form.Sess6_1) ) {
errors[errors.length] = "Please indicate your first choice for Session 6.";
}
}
if (SunSess != "off") {
if ( !checkSelect(form.Sess6_2) ) {
errors[errors.length] = "Please indicate your first choice for Session 6.";
}
}
if (errors.length > 0) {
reportErrors(errors);
return false;
}
return true;
}
</script>
<!--- End Registration Form Validation --->
For some reason the if statements.....if (SatSess != "off") and if (SunSess != "off") won't validate as true in Firefox or IE but they will in Chrome?? I had the same issue when when I try.... if (room == "Double").
What am I doing wrong??
It's maybe a stupid question, but it's my first year working with something like javascript.
I got some alert boxes, and I was wondering if there any possiblility to show only one alert box (in javascript) with all the stuff I want them to do.
And when they fill in one of the inputs or buttons, that the allert will only show the other missing things.
(I'm getting an alert of the first code. When I fill it in I get an alert of the next code, and so on. I want to have all in one.)
/*validate name*/
var n=document.forms["check"]["name"].value;
if(n==null||n=="")
{
alert("Please, fill in your name.");
return false;
}
/*validate the sex*/
if(document.getElementById('male').checked)
{
}
else if(document.getElementById('female').checked)
{
}
else
{
alert("Please, enter your gender.");
return false;
}
/*validate the E-mail*/
var e=document.forms["check"]["email"].value;
var atpos=e.indexOf("#");
var dotpos=e.lastIndexOf(".");
if(e==null||e=="")
{
alert("Please, fill in your e-mail.");
return false;
}
if(atpos<1 || dotpos<atpos+2 || dotpos+2>=e.length)
{
alert("This isn't a valid e-mail address.");
return false;
}
/*validate agreement*/
if(document.getElementById("I don't want my information to be part of this website.").checked)
{
}
else if(document.getElementById("I wish to be registered.").checked)
{
}
else if(document.getElementById("I wish to get the new content of this website.").checked)
{
}
else
{
alert("Please, tell us what we can do with your information.");
return false;
}
/*validate the terms*/
if(document.getElementById("yes").checked)
{
}
else if(document.getElementById("no").checked)
{
alert("You have to agree with the terms.");
return false;
}
else
{
alert("Please, enter the terms.");
return false;
}
// initialise an array to populate along the way
var alerts = [];
/*validate name*/
var n = document.forms[ "check" ][ "name" ].value;
if ( n == null || n == "" ) {
// push message onto the array
alerts.push( "Please, fill in your name." );
return false;
}
/*validate the sex*/
if ( document.getElementById( 'male' ).checked ) {} else if ( document.getElementById( 'female' ).checked ) {} else {
// push message onto the array
alerts.push( "Please, enter your gender." );
return false;
}
/*validate the E-mail*/
var e = document.forms[ "check" ][ "email" ].value;
var atpos = e.indexOf( "#" );
var dotpos = e.lastIndexOf( "." );
if ( e == null || e == "" ) {
// push message onto the array
alerts.push( "Please, fill in your e-mail." );
return false;
}
if ( atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= e.length ) {
// push message onto the array
alerts.push( "This isn't a valid e-mail address." );
return false;
}
// join up the array of messages, and alert the user...
alert(alerts.join(", "));
In summary...
// initialise an array to populate along the way
var alerts = [];
...
// push messages onto the array
// (repeat this step for all messages)
alerts.push( "Any validation message" );
...
// join up the array of messages, and alert the user...
alert(alerts.join(", "));
I am taking three text boxes. time in text boxes are not to be same. means 1st text box value is 2013-10-01 12:00 date time.and second is 2013-10-01 12:00 and third also 2013-10-12 12:00.but actual problem is that when date are diff-rent then same time should be allow.when date and time are same so it should through error message or alert message to user.please help me to solve this.
function validate Form()
{
var a=document.get Element By Id("mybox1"). value;
var b=document.get Element By Id("mybox2"). value;
var c=document.get Element By Id("mybox3"). value;
var a_time = a.replace(/ /g,''). sub st r (a.replace(/ /g,''). length - 5);
var b_time = b.replace(/ /g,''). sub st r (b.replace(/ /g,''). length - 5);
var c_time = c.replace(/ /g,''). sub st r (c.replace(/ /g,''). length - 5);
if (a=="" && b=="" && c=="")
{
alert("Please select at least one date and time !");
return false;
}
else if (a_time === b_time)
{
alert("Please select diff-rent time!");
return false;
}
else if (a_time === c_time)
{
alert("Please select diff-rent time!");
return false;
}
else
{
return true;
}
}
Try this... only need to compare date too...
if (a=="" && b=="" && c=="")
{
alert("Please select at least one date and time !");
return false;
}
else if (a_time === b_time && a===b)
{
alert("Please select diff-rent time!");
return false;
}
else if (a_time === c_time && a===c)
{
alert("Please select diff-rent time!");
return false;
}