Javascript: Assigning a variable and testing it at the same time [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In PHP, we can assign a variable AND test it at the same time:
<?php
if ($result = $this->find()) {
$this->do_something($result);
}
function find() {
if ($this->day == 'Sunday')
return '1234';
else
return false;
}
In the above example, on Sundays, $result is set to '1234' and calls the do_something function. On other days, it is set to false and nothing else occurs.
Is this kind of thing possible with Javascript?

Yes, it is possible.
var day=[
'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday',
][new Date().getDay()];
if(result = find()){
do_something(result);
}
function find(){
if(day == 'Sunday')
return '1234';
else
return false;
}
Tada! Calls do_something on Sundays.

Related

Code always showing same output (Problem with If statements in JavaScript) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried making a tool that calculates battle out comes but this part of the code always displays: "DRAW!"
function battle()
{
var rawpower = document.getElementById('rawpower').value;
var rawpoweropp = document.getElementById('rawpoweropp').value;
if(rawpower > rawpoweropp){
alert("You won!");
} else if(rawpower < rawpoweropp){
alert("You lose!");
} else{
alert("Draw!");
}
}
The element with id="rawpower" is a paragraph tag <p>. These elements do not have values. So document.getElementById('rawpower').value returns undefined, and same for the other line. undefined is not less than undefined, nor is it greater than undefined, so you're going into the third case.

Explanation of Regular Expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone explain what is meaning of this Regular Expression?
/^(https?):\/\/(www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z0-9]{2,3}[a-zA-Z0-9\-\#\.\/\?]*$/
Finally I have founded my solution:
function validateURL(url)
{
var re = /^(https?):\/\/(www\.)?[a-z0-9\-\.]+\.[[a-z0-9\-\.\/]{2,}]*$/;
if (!re.test(url))
{
return false;
}
else
{
return true;
}
}
this function return true if your url contain these:
part 1. https or http
part 2. www or not (means optional).
Below image exploring in more depth.
I am wrong?
I've tried the following and works fine for me.
var url = "https://www.xyz.xe";
function validateURL(url)
{
var urlregex = new RegExp("^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*#)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test(url);
}
validateURL(url);
Returs false when url="https://www.xyz.x" and true when url="https://www.xyz.xe"

How to validate in Jquery? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to jQuery. I have a text field and I want to validate this field so that it only takes character and alphanumeric values, however if you put in a numeric value it will show a message. How should I implement this?
$('#textfieldid').on('change', function(event) {
var text = $('#textfieldid').text();
//some validation code here
});
By the way, JQuery is a javascript library, not Java (which you have tagged).
Using regex, validations can be done.
var password = document.getElementB;
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
if(number.test(password) && letter.test(password)){
//success
}else{
//error
}
Here password is the value which is returned from the field

javascript function return clarification required [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In this function, am expecting the return value to be increment of variable value . However , i am getting original value ,
here is my function:
var num = function(){
var a = 0;
return a++;
}
alert(num()); //it giving the result as 0 instead of 1...why?
can anyone please explain this to me?
The ++ acts after the 'return':
return a++ ==> return a, then add 1 to a
return ++a ==> add 1 to a, then return
Look at this answer.
try
var num = function(){
var a = 0;
return ++a;
}
++a give the value after operation a++ give the value then do the operation

Object doesn't support property or method 'ajaxSubmit' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have this issue when im trying to upload a form in ie: Object doesn't support property or method 'ajaxSubmit' Here is my code:
function Upload() {
ShowWait();
uploadCompleted = false;
$(document.forms[0]).ajaxSubmit({ success: PostUploadProcess });
intervalHandler = setInterval(function () {
var responseText = $("iframe").contents().find("body").html();
if (responseText)
PostUploadProcess(responseText);
}, 1000);
return false;
}
Can anyone help me?
http://malsup.com/jquery/form/
needs to be separately included. its not part of jquery core.

Categories