I'm trying to get a function to run an if statement only if something has no value.
<script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
function checkNSC() {
var confirmNSC = $("#SelectNsc");
if (confirmNSC.length == 0) {
alert("Please ensure all required fields are filled out")
}
else {
alert("NSC has value")
}
}
</script>
HTML:
<input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</div>
I'm trying to say if the value is 0 then to display an alert saying (Please ensure all fields are filled out". Otherwise say "NSC has a value"
Thanks,
Should be,
var confirmNSC = $("#SelectNsc").val();
Because $("#SelectNsc") return a jquery object, so that $("#SelectNsc").length will always return a value greater than 0.
<script src = "https://jqueryvalidation.org/files/lib/jquery-1.11.1.js" > < /script>
<script type = "text/javascript" >
$(document).ready(function() {
$("#myButton").click(function() {
var confirmNSC = $("#SelectNsc").val().trim();
if (confirmNSC.length == 0) {
alert("Please ensure all required fields are filled out")
} else {
alert("NSC has value")
}
});
})
</script>
Fiddle
Try this, It's working fine
<script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
function checkNSC() {
var confirmNSC = $("#SelectNsc");
if (confirmNSC.val() =='') {
alert("Please ensure all required fields are filled out")
}
else {
alert("NSC has value")
}
}
</script>
<input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</div>
use this code :
<body>
<script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
function checkNSC() {
var confirmNSC = document.getElementById("SelectNsc").value;
if (confirmNSC.length == 0) {
alert("Please ensure all required fields are filled out")
}
else {
alert("NSC has value")
}
}
</script>
<input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</body>
OR , use this code :
<body>
<script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
function checkNSC() {
var confirmNSC = $("#SelectNsc").val();
if (confirmNSC.length == 0) {
alert("Please ensure all required fields are filled out")
}
else {
alert("NSC has value")
}
}
</script>
<input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</body>
Related
So I tried to make a key input that when users write something, prefix to be geton_ and random characters next... For example: geton_adminloginpanel or geton_lgpanel.
Tried this but not work like I thought:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>KeyPanel</title>
</head>
<body>
<span>Enter The Key</span>
<input aria-label="Enter the key" id="key-text" placeholder="Key" value="">
<button role="button" value="submit" id="key-btn" onclick="fctbtnkeygetorerror()">LOGIN</button>
</body>
</html>
JAVASCRIPT:
<script>
function fctbtnkeygetorerror() {
if (document.getElemetById("key-text").value == "geton_" + document.getElemetById("key-text").value) {
alert("Successfully Logged In");
} else {
alert("Invalid Key");
}
}
</script>
<script>
function fctbtnkeygetorerror() {
main_password = "geton_Password";
prefix = "geton_";
input_value = document.getElementById("key-text").value;
new_value = prefix+input_value;
if(new_value==main_password){
alert("Login Success");
}else
{
alert("Invalid Login");
}
}
</script>
I want to know how to detect if a textbox does not contain certain words. For example if the textbox did not contain 'who', 'what', 'why', 'when' or 'where' some sort of function would run.
JavaScript:
function command() {
var srchVar = document.getElementById("srch");
var srch = srchVar.value;
var t = srch;
if (srch == '') {
alert('Please do not leave the field empty!');
}
else if (srch.indexOf('time') != -1) {
alert('The current time according to your computer is' + ShowTime(new Date()));
}
else if (srch.indexOf('old are you') != -1) {
alert("I am as old as you want me to be.");
}
else {
if (confirm('I am sorry but I do not understand that command. Would you like to search Google for that command?') == true) {
window.open('https://google.co.uk/#q=' + srch, '_blank');
}
else { /* Nothing */ }
}
}
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="script.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<div class="ui-widget">
<input class="search-field" id="srch" onkeypress="searchKeyPress(event);" placeholder="ask me anything" spellcheck="false">
</div>
</body>
</html>
You will want to call that function when you press a button.
Try something in your HTML like:
<input class="info" onclick="contains();" type="button">
And in your JS
function contains() {
var srchVar = document.getElementById("srch");
var srch = srchVar.value;
if (
(srch.indexOf('who')==-1) &&
(srch.indexOf('what')==-1) &&
(srch.indexOf('why')==-1) &&
(srch.indexOf('when')==-1) &&
(srch.indexOf('where')==-1)
) {
alert("That is not a question");
}
}
You will want to merge this with your command() function in the future.
Also, what does info(); do ?
Here I have many input text fields with same class like
<input type="text" class="MyClass"></input>
<input type="text" class="MyClass"></input>
<input type="text" class="MyClass"></input>
My requirement is to check wheather all input fields of this class is empty or not.
I've tried this
if(!('.MyClass').val()){
alert("empty");
}
But it doesn't make any result. Can anyone help?
You can check
$('button').click(function() {
var $nonempty = $('.MyClass').filter(function() {
return this.value != ''
});
if ($nonempty.length == 0) {
alert('empty')
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<button>test</button>
Or using a flag
$('button').click(function() {
var flag = false;
$('.MyClass').filter(function() {
if (this.value != '') {
flag = true;
//no need to iterate further
return false;
}
});
if (!flag) {
alert('empty')
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<button>test</button>
You can compare the length of number of input with number of empty input using :
var allemptylength=$(".MyClass").filter(function() {
return this.value.length !== 0;
})});
if($('.MyClass').length== allemptylength.length){
alert("all empty");
}
Working Demo
To make sure each classes are checked, use
$.each($('.MyClass'),function() {
if ($(this).val().length == 0) {
alert('empty');
}
});
You can check it like this:
var isEmpty = !$('.MyClass').filter(function() {
return this.value.trim();
}).length;
Also remove all </input> tags. input elements are self-closable.
try this
var hasNoValue;
$('.MyClass').each(function(i) {
if ($(this).val() == '') {
hasNoValue = true;
}
});
if (hasNoValue) {
alert('All have no value');
}
try is(':checked')
$('.MyClass').each(function(){
alert($(this).is(':checked');)
});
this will alert only the checked elements.
$('input').each(function(index,value){
if($(this).val()!=''){
$(this).addClass('success');
}else{
$(this).removeClass('someClass');
$(this).addClass('warning');
$(this).css('border', '1px solid red');
//do something else...
}
});
Try this . 100% working..
var emptyLength = $(".MyClass").filter(function() {
return this.value == "";
}).length;
if (emptyLength > 0)
{
alert("Please enter all peramerter's value");
return;
}
Thanks.
Try this. It works for me
var flag = 1;
$(".MyClass").each(function(i){
if ($(this).val() == "")
flag++;
});
if (flag == 1)
alert('all have values');
else
alert('some or all doesn\'t have value');
i need to make an html page when you put a password to. whenever the user insert the correct password he will be sent out to a website. if the user won't insert the correct password he will get a an error.(i need to do it with only javaScript.no jquery and stuff like that) this is what i did -
<form>
Write the password here:
<input type="text" id="putPass" name="go2" /> <br />
<input type="button" value="click here" id="press" onclick="pass()" />
</form>
<script type="text/javascript">
var password = ["1234","abcd","0000","1111","4321"]
function pass()
document.getElementById("putPass").value. //it says it has a syntax error and i dont know why...
if (this == password[])
{
document.write("good job!");
}
else
{
alert("try again!");
}
</script>
thank you.
You can try to do that using this code.
http://jsfiddle.net/VAK3N/
<form>
<input type="text" id="putPass" name="go2" />
<br />
<input type="button" value="click here" id="press" />
</form>
<script type="text/javascript">
var password = ["1234", "abcd", "0000", "1111", "4321"];
document.getElementById('press').onclick = function () {
var p = document.getElementById("putPass").value;
if (password.indexOf(p) > -1) {
alert("good job!");
} else {
alert("try again!");
}
}
</script>
Try this
<script type="text/javascript">
var password = ["1234","abcd","0000","1111","4321"];
function pass(){
var userPass = document.getElementById("putPass").value;
if (in_array(userPass, password)){
document.write("good job!");
}
else {
alert("try again!");
}
}
function in_array(needle, haystack){
for(var key in haystack)
{
if(needle === haystack[key])
{
return true;
}
}
return false;
}
</script>
var pass = document.getElementById("putPass").value;
var correct = false;
for (var i=0;i<password[].length;i++){
if (password[i] == pass){
correct = true;
break;
}
}
if (correct){
document.write("good job!");
} else {
alert("try again!");
}
Not sure if that exact code will work, but I think that you need to check each password individually in a for loop, you can't compare strings with entire arrays.
I didn't see first that you dont want to use jQuery. So then here is the working code for you.
JS:
var password = ["1234","abcd","0000","1111","4321"]
function pass(){
var input = document.getElementById('putPass').value;
var accepted= false;
for(var pass in password){
if(password[pass] == input ){
accepted= true;
}
}
if(accepted){
alert('good job!');
}else{
alert('try again!');
}
}
HTML:
<form>
<input type="text" id="putPass" name="go2" /> <br />
<input type="button" value="click here" onClick="pass();" id="passSender"/>
</form>
How would I fix,"Expected End Of Statement" in the code below?
<script type="text/javascript">
function substitute() {
var myValue = document.getElementById('myTextBox').value;
if (myValue.length === 0) {
alert('Please enter a real value in the text box!');
return;
}
var myTitle = document.getElementById('title');
myTitle.innerHTML = myValue;
}
</script>
It keeps telling me line 57 which is this line:
<input type="submit" value="Click Me" onClick="substitute();">
Here is the complete HTA link:
http://pastebin.com/fMg5e4RN
Use <form onsubmit="return substitute()" and return true or false depending on validation. Remove type="javascript" or fix it as text/javascript
<script type="text/javascript">
function substitute() {
var myValue = document.getElementById('myTextBox').value;
if (myValue.length === 0) {
alert('Please enter a real value in the text box!');
return false;
}
// not sure what the following two lines are for
var myTitle = document.getElementById('title');
myTitle.innerHTML = myValue;
return true; // allow submit
}
</script>
and use
<form action="some action" onsubmit="return substitute();">
<input type="text" id="myTextBox"/>
<input type="submit" value="Click Me" />
</form>