Cannot get form to validate [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
The problem is nothing validates. I can leave the form empty and it gives no alert
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function validateForm() {
var x = document.forms["myform"]["fname"].value;
if (x == null || x == "") {
alert("Enter your certificate address first.");
} else if(!x.match(/facebook/g) || !x.match(/access_token/g)) {
alert("Invalid certificate address, please try again...");
return false;
}
)
</script>
<form name="myform" action="welcome.php" method="post" onsubmit="return validateForm();">
Name: <input type="text" name="fname">
<input type="submit" value="submit">
</form>
</body>
</html>
not sure what is wrong with this. looks pretty straitforward

Missing your last end bracket. Changes ) to }

Related

Javascript in HTML document is not executed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
This is a copy of an HTML document from a tutorial on Javascript. Upon opening it in Chrome, it doesn't behave as intended. When I click the button with Set Cookie written on it, nothing shows up, regardless of whether I have written something in the box (we should get output on the page) or not (we should get an alert). Why?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie()
{
if( document.myform.customer.value == ""){
alert("Enter a name");
return;
}
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie="name=" + cookievalue;
document.write("Setting Cookies : " + document.cookie);
}
//-->
</script>
</head>
<body>
<form name="myform" action ="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onlick="WriteCookie();"/>
</form>
</body>
</html>
You have a typo in your input box. It should read:
<input type="button" value="Set Cookie" onClick="WriteCookie();"/>

Validation of percentage in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i can't seems to validate the percentage.
Need you guys help me to solve!
function validate()
{
s
if( document.myForm.outputPercentage.value <= 0||>=100
{
alert( "Please the correct percentage." );
document.myForm.outputPercentage.focus() ;
return false;
<form name="myForm" onsubmit="return(validate());">
Last output percentage: <input type"text" name="outputPercentage" id="outputPercentage" />&<br/><br/>
<input type="submit" value="Submit" />
I've corrected your code so that it works:
<html>
<head>
<script type="text/javascript">
function validate() {
var outputPercentageString = document.myForm.outputPercentage.value;
var outputPercentage = parseInt(outputPercentageString);
if (outputPercentage <= 0 || outputPercentage >= 100) {
alert("Please the correct percentage.");
document.myForm.outputPercentage.focus();
return false;
}
}
</script>
</head>
<body>
<form name="myForm" onsubmit="return(validate());">
Last output percentage: <input type="text" name="outputPercentage" id="outputPercentage" />%
<br/><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Remove the 's' all on its own.
Everything on a web page is a String, so to compare the percentage to a number you have to use parseInt to make it into a number.
type"text" should be type="text"
if you open a bracket { then you must close it afterwards }
Put the javascript into <script> tags in the <head> element of the page
I've put everything into <html> elements.
Learn what getElementById() does. Everybody uses ids to get references to webpage elements rather than names.
Hope this helps.

Check if a text box starts with 92 using 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 7 years ago.
Improve this question
<form name="home" action="" onsubmit="return Validateform();" method="post">
<input type="text" name="receiver" />
<input type="submit" value="send" name="send" />
</form>
What is the javascript to alert if the textbox does not start with 92 on submit?
I recommend you read a JavaScript tutorial. Try here.
To answer your question, this will probably work. If you're wondering why you're getting downvoted - asking people to write your code for you is generally considered bad form, so try not to do this in future. :)
function Validateform(e) {
if (document.getElementsByName('receiver')[0].value.substring(0, 2) !== "92") {
e.preventDefault();
alert("Alert");
}
}

Javascript Incomplete tags [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
I work for a nascent online retailer in Ghana, West Africa. We purchased an E-commerce template from Big Commerce. On our website there's a text-field where users are allowed to enter their email addresses and receive newsletters subsequently. Unfortunately, when users enter information, its not seen at the back end. They don't receive the automatic email once they hit enter. i suspect there's something wrong with Javascript code. Please advise:
<script type="text/javascript">
//
$('#subscribe_form').submit(function() {
if($('#nl_email').val() == '') {
// alert('You forgot to type in your email address.');
$('#nl_email').focus();
return true;
}
if($('#nl_email').val().indexOf('#') == -1 || $('#nl_email').val().indexOf('.') == -1) {
//alert('Please enter a valid email address, such as john#example.com.');
$('#nl_email').focus();
$('#nl_email').select();
return true;
}
// Set the action of the form to stop spammers
$('#subscribe_form').append("<input type="hidden" name="check" value="1" />");
return true;
});
</script>
Syntax error here
$('#subscribe_form').append("<input type="hidden" name="check" value="1" />");
Fix:
$('#subscribe_form').append("<input type='hidde' name='check' value='1' />");
Try putting:
$('#subscribe_form').append("<input type="hidden" name="check" value="1" />");
as
$('#subscribe_form').append('<input type="hidden" name="check" value="1" />');
or it will raise an error because of the quotes...

clearing an input with javascript [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 want to clear the email input box when function(data) is greater than zero. The user will have to be force to re-enter another email address since the form will not submit if any fields are blank. how would I clear out the input box?
javascript:
$("#email").change(function(){
var email=$("#email").val();
$.ajax({
type:"post",
url:"/files/reg_check.php",
data:"email="+email,
success:function(data){
if(data == 0){
$("#email_msg").html("E-mail Address must be valid");
} else {
$("#email_msg").html("<div class='msg_check_red'>Error: E-mail address already in use.</div>");
}
}
});
});
html:
<label>E-mail:</label>
<input type="email" placeholder="your#email.com" id="email" name="add[email]" size="20" title="A valid email address required" class="{validate:{required:true,email:true}}" />
<span class="rsmall"><div id="email_msg">E-mail Address must be valid</div></span><br />
Use $("#email").val("");, which sets the value of the input to an empty string.

Categories