Check Passwords Match Issue (JavaScript) - javascript

can anybody see the issue with the following code? My script continues, ignoring my check.
The code below is only a snippet of the code but this issue is in here somewhere after doing some debugging.
function validatePasswordMatch(string1, string2) {
if (string1 != string2) {
return false;
} else {
return true;
}
}
var validatePasswordMatchFields = ["textbox2", "textbox3"];
if (validatePasswordMatch($("#" + validatePasswordMatchFields[0]).val(), $("#" + validatePasswordMatchFields[1]).val())) {
(some code)
} else {
return false;
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>stackoverflow</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
function validatePasswordMatch(string1, string2) {
if (string1 !== string2) {
return false;
} else {
return true;
}
}
var validatePasswordMatchFields = ["textbox2", "textbox3"];
$(document).on('click','button',function(){
result = validatePasswordMatch($("#" + validatePasswordMatchFields[0]).val(), $("#" + validatePasswordMatchFields[1]).val())
if (result) {
alert("match")
} else {
alert("not match")
}
})
});
</script>
</head>
<body>
<input id="textbox2" type="text" placeholder="password">
<input id="textbox3" type="text" placeholder="password">
<button type="button" id="button">Click Me!</button>
</html>

Related

How to make an input for key verify with any prefix?

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>

Cheker to clean a string

I'm doing a small script with jquery, I'd like, that by clicking on the checkbox, I'll take the first textarea and pass it to the 2 text area without numbers, but, that textarea 1 will be as it is in 2
$(document).ready(function() {
function validate() {
if (document.getElementById('cheker').checked) {
results = document.getElementById("all").value;
final = results.string.replace(/\d+/g, '');
document.getElementById("filtrado").value(final);
} else {
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="all"></textarea>
<input id="checker" name="checker1" type="checkbox" />
<textarea id="filtrado"></textarea>
"value" is a property , as mentioned by jonas and you have a wrong in id "cheker > checker" , try this
$(document).ready(function() {
function validate() {
if (document.getElementById('checker').checked) {
results = document.getElementById("all").value;
final = results.replace(/\d+/g, '');
document.getElementById("filtrado").value = final;
} else {
}
}
});
No need to call string on a variable that is already a string, that and some typos and LGSon's comment, try this
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
function validate() {
if (document.getElementById('checker').checked) {
results = document.getElementById("all").value;
final = results.replace(/\d+/g, '');
document.getElementById("filtrado").value=final;
} else {
}
}
$('#checker').change(function(){
validate()
});
});
</script>
</head>
<body>
<textarea id="all"></textarea>
<input id="checker" name="checker1" type="checkbox" />
<textarea id="filtrado"></textarea>
</body>
</html>

disable submit button if there is validation errors in form inputs with jquery

I want to disable submit button if there is a validation error from form inputs using jquery. The following is code that I am using:
HTML:
<form action="/order" id="orderForm" class="orderform" autocomplete="off" method="post" accept-charset="utf-8">
<div class="orderform-inner">
<ol class="questions" id="questions">
<li>
<span><label for="oName">Please enter your name or the company name</label></span>
<input class="finput" id="oName" name="oName" type="text" maxlength="20"/>
<span class="input-error"></span>
</li>
</ol>
</div>
<button name="submit" class="submit" type="submit">Submit</button>
</form>
JS:
function _validate(input) {
if( input.val() === '' ) {
_showError(input, 'EMPTYSTR');
return false;
}
return true;
}
function _checkForm() {
$('#orderForm .finput').each(function(){
$(this).focusout(function() {
_validate($(this));
});
});
}
$(document).ready(function() {
_checkForm()
$('form#orderForm').submit(function(event) {
event.preventDefault(); // for ajax submission
if(!_checkForm()) {
$('button.submit').prop('disabled', true);
}
else {
// ajax post
}
});
});
Update: There is no problem with disabling button. The problem is that after correcting errors, again the disabled attribute remains! What am I doing wrong?
You are not returning a result from the _checkForm(){} function. You do from the _validate one, and pass it to it, but you don't use/pass the result from _checkForm(), therefore this validation:
if(!_checkForm()) {...}
is always true, because _checkForm returns nothing(undefined) and your !-ing it. Also, if the check passes, you should return false to break the submit.
You forget the return false.
please try this:
function _validate(input) {
if( input.val() === '' ) {
_showError(input, 'EMPTYSTR');
return false;
}
return true;
}
function _checkForm() {
$('#orderForm .finput').each(function(){
$(this).focusout(function() {
_validate($(this));
});
});
}
$(document).ready(function() {
$('form#orderForm').submit(function(event) {
event.preventDefault(); // for ajax submission
if(!_checkForm()) {
$('button.submit').prop('disabled', true);
return false;
}
else {
// ajax post
}
});
});
test1.html
function _validate(input) {
if( input.val() === '' ) {
_showError(input, 'EMPTYSTR');
return false;
}
return true;
}
function _checkForm() {
$('#orderForm .finput').each(function(){
$(this).focusout(function() {
_validate($(this));
});
});
}
$(document).ready(function() {
_checkForm();
$('form#orderForm').submit(function(event) {
event.preventDefault(); // for ajax submission
if(!_checkForm()) {
$('button.submit').prop('disabled', true);
}
else {
// ajax post
}
});
});
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<form id="orderForm">
<input class="finput">
<button class="submit">submit</button>
</form>
<script type="text/javascript" src="test1.js"></script>
</body>
</html>

Detect if a textbox does not contain certain words

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 ?

How to check if a html control exists?

How do I check if an HTML control exists?
This is my code:
var id = ...
if(document.getElementById(id)!=null)
{
//do something
}
if the html control is radio doesn't work.
How is this done?
Further to your comments above, you should use a unique id for each radio button, but you can use the same name for your radio group. Consider the following example:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Testing document.getElementById()</title>
</head>
<body>
<input type="radio" id="my_radio_1_id" name="my_radios"> My Radio Button 1
<input type="radio" id="my_radio_2_id" name="my_radios"> My Radio Button 2
<script type="text/javascript">
if (document.getElementById('my_radio_1_id')) {
alert('It Exists');
}
</script>
</body>
</html>
It will alert 'It Exists' as expected.
function validate(ValFrm) {
var len = ValFrm.elements.length;
for (i = 0; i < len; i++) {
if (ValFrm.elements[i].required == "Yes") {
if (ValFrm.elements[i].value == "") {
alert('The ' + ValFrm.elements[i].title + ' is required to contain data.');
ValFrm.elements[i].focus();
return false;
}
}
if (ValFrm.elements[i].type == "radio") {
if (!ValFrm.app[0].checked && !ValFrm.app[1].checked) {
alert('Please accept or deny.');
return false;
}
}
}
}

Categories