Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using the following script to validate a form and would like to be able to to ensure that the inputter correctly inputs a numeric value with decimal places on the last field (amount).
Can anyone help with this?
<script type="text/javascript">
<!--
function validate()
{
if( document.create_invoice.job_no.value == "" )
{
alert( "Please provide a Job No" );
document.create_invoice.job_no.focus() ;
return false;
}
if( document.create_invoice.contact.value == "-1" )
{
alert( "Please provide a Customer Contact" );
return false;
}
if( document.create_invoice.employee.value == "-1" )
{
alert( "Please provide an Employee" );
return false;
}
if( document.create_invoice.invoice_no.value == "" )
{
alert( "Please provide an Invoice No" );
document.create_invoice.invoice_no.focus() ;
return false;
}
if( document.create_invoice.address.value == "" )
{
alert( "Please provide an Address" );
document.create_invoice.address.focus() ;
return false;
}
if( document.create_invoice.amount.value == "" )
{
alert( "Please provide an Amount" );
document.create_invoice.amount.focus() ;
return false;
}
}
//-->
</script>
Thanks,
John
You might want to use parseFloat() for that... Try :
if( parseFloat(document.create_invoice.amount.value) != document.create_invoice.amount.value )
{
alert( "Please provide a decimal value" );
document.create_invoice.amount.focus() ;
return false;
}
That might just do the trick for you.
Related
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 8 months ago.
Improve this question
This is my javascript but it works fine with all the other fields when nothing is entered I'm trying to make the user need to enter at least 8 characters before submitting but currently, they can submit on 0.
function validateForm() {
if (document.register.firstName.value == "") {
alert("Please provide your first name!");
document.register.firstName.focus();
return false;
}
if (document.register.secondName.value == "") {
alert("Please provide your second name!");
document.register.secondName.focus();
return false;
}
if (document.register.email.value == "") {
alert("Please provide your Email!");
document.register.email.focus();
return false;
}
if (document.register.phoneNo.value == "") {
alert("Please provide your Phone Number!");
document.register.phoneNo.focus();
return false;
}
if (document.register.Gender.value == "") {
alert("Must select a Gender!");
document.register.Gender.focus();
return false;
}
if (document.register.Terms.value == "") {
alert("Terms and Conditions must be accpeted");
document.register.Terms.focus();
return false;
}
if (document.register.psw.value.length > 7) {
alert("Password must contain 8 character");
document.register.psw.focus();
return false;
}
return (true);
}
You need to check if it's <= 7 characters in length (or probably < 8 for clarity). Currently you're preventing the user from submitting a password if it's too long, not the other way around:
if (document.register.psw.value.length < 8) {
...
}
I'm assuming you're using the password input field. If so, you can handle that from the HTML by doing something like this (see below) and have the browser handle the validation.
<input type="password" minlength="4" maxlength="8">
Just try the following. Notice, as you mentioned, it is not greater than 7, but less than 8.
if( document.register.psw.value.length < 8) {
alert( "Password must contain 8 character" );
document.register.psw.focus() ;
return false;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
<SCRIPT LANGUAGE="JavaScript">
function num_only()
{
var keyASCII = window.event.keyCode;
var keyValue = String.fromCharCode(keyASCII);
if (!(keyValue >= '0' && keyValue <= '9'))
{
window.event.keyCode=0;
alert ("please enter numeric value");
}
}
function check()
{
var td=document.dfrm.tdd.value; var tm=document.dfrm.tmm.value; var ty=document.dfrm.tyy.value;
var fd=document.dfrm.fdd.value; var fm=document.dfrm.fmm.value; var fy=document.dfrm.fyy.value;
if(td=="" ){
alert("Please Enter All Fields");
//td.select();
return false;
}
else if(tm=="" ){
alert("Please Enter All Fields");
//tm.select();
return false;
}
else if (ty=="") {
alert("Please Enter All Fields");
//ty.select();
return false;
}
else if(fd=="") {
alert("Please Enter All Fields");
//fd.select();
return false;
}
else if(fm=="" ){
alert("Please Enter All Fields");
//fm.select();
return false;
}
else if( fy==""){
alert("Please Enter All Fields");
//fy.select();
return false;
}
}
</SCRIPT>
If this is the verbatim copy of your Javascript file, remove the script tags surrounding your script.
The script tags are not valid JS and are only used in HTML files.
alright, whats wrong with my script, JShint is throwing three lines of red code saying i have issues in my variable and my if/else statement. I am trying to validate if the phone number is a valid USA phone format
$( "#phone" ).focusout(function telephone() {
if( this.value === "" || this.value === null ) {
$( "#error_messages" ).text("");
return false;
} else {
var re = \d{3}[\-]\d{3}[\-]\d{4};
if(re.test(document.getElementById("phone").value)) {
$( "#error_messages" ).text("");
return true;
} else {
$( "#error_messages" ).text("Phone Number* not a valid format xxx-xxx-xxxx");
return false;
}
}
});
Firstly, you don't need a name for closure function. Secondly you have not enclosed regex with /..../.
var re = /\d{3}[\-]\d{3}[\-]\d{4}/;
You Should try this.
$(function(){
$("#phone").change(function telephone() {
if (this.value === "" || this.value === null) {
$("#error_messages").text("");
return false;
} else {
if (/\d{3}[\-]\d{3}[\-]\d{4}/.test($(this).val())) {
$("#error_messages").text("");
return true;
} else {
$("#error_messages").text("Phone Number* not a valid format xxx-xxx-xxxx");
return false;
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="text" id="phone" />
<span id="error_messages"></span>
This is an expression tested on http://tools.netshiftmedia.com/regexlibrary/# for numbers. change it to /^\d{3}[\-]\d{3}[\-]\d{4}$/ in your JS code and apply regex code within /..../ in that tools
I applied
^\d{3}[\-]\d{3}[\-]\d{4}$
It's ok with your numbers :
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to show an alert message when the check box is not selected. I use the following code for that purpose
function IsEmpty(){
var oldpath = document.forms['pathuploader'].oldpath.value;
var newpath = document.forms['pathuploader'].newpath.value;
var metavalue = !document.forms['pathuploader'].chkmeta.checked;
var postvalue = !document.forms['pathuploader'].chkpost.checked;
if((oldpath == "")||((oldpath.substring(0,4))!='http')||((oldpath.substring(0,4))=='Http'))
{
alert("Enter a valid URL");
return false;
}
if((newpath == "")||(newpath.substring(0,4)!='http')||(newath.substring(0,4)!='Http'))
{
alert("Enter a valid URL");
return false;
}
if((metavalue) && (postvalue))
{
alert("Select any category to change");
return false;
}
return true;
}
Working JSFiddle
First of all you have a typo on the following line
if((newpath == "")||(newpath.substring(0,4)!='http')||(newath.substring(0,4)!='Http'))
The last if is "newath" should be "newpath" and the same area "!=" should match the oldpath logic and instead be "==".
To clean up the code just a bit more, use "===" and "!==" instead of just "==" as this forces a more precise comparison.
See this link for more info use strict mode
Here is adjusted code
Also, try to use a camelCase naming convention if you wish to comply with JS standards. I have corrected the "IsEmpty" function to be "isEmpty" as an example.
function isEmpty(){
var oldpath = document.forms['pathuploader'].oldpath.value;
var newpath = document.forms['pathuploader'].newpath.value;
var metavalue = !document.forms['pathuploader'].chkmeta.checked;
var postvalue = !document.forms['pathuploader'].chkpost.checked;
if((oldpath === "")||((oldpath.substring(0,4))!=='http')||((oldpath.substring(0,4))==='Http'))
{
alert("Enter a valid old URL");
return false;
}
if((newpath === "")||(newpath.substring(0,4)!=='http')||(newpath.substring(0,4)==='Http')){
alert("Enter a valid new URL");
return false;
}
if((metavalue) && (postvalue)){
alert("Select any category to change");
return false;
}
return true;
}
UPDATE I also agree with "Sourabh" where the BANG (!) should be. As in
if(( !metavalue ) && ( !postvalue ){
instead of how it is currently. Both work, but the BANG is hiding in the variable. If you did keep it where it is, perhaps you could alert the next programmer that may view your code by calling it
var metaValueNotChecked = !document.forms...
var postValueNotChecked = !document.forms...
Then it would read correctly as
if(( metaValueNotChecked ) && ( postValueNotChecked ){
In this case, the BANG should be where you have it.
Hope this helps!
use the below procedure for more better way to do it, i am assuming that you have elements defined in your form, you need to change this two parts of code
first:
var metavalue = document.forms['pathuploader'].chkmeta.checked;
var postvalue = document.forms['pathuploader'].chkpost.checked;
then in if condition use the below procedure:
if(!metavalue && !postvalue)
{
alert("Select any category to change");
return false;
}
You can use "required" from HTML5, and remove it once a checkbox is checked, from every other of your checkbox. ex:
<input required="required" value="1" name="site[source][]" id="site_source_any" type="checkbox">
<input required="required" value="2" name="site[source][]" id="site_source_many" type="checkbox">
In your script file:
<script type="text/javascript">
// Check if atleast one of the checkbox is checked
$(function(){
var requiredCheckboxes = $(':checkbox[required]');
requiredCheckboxes.change(function(){
if(requiredCheckboxes.is(':checked')) {
// Remove Required once at-least one is checked
requiredCheckboxes.removeAttr('required');
}
else {
requiredCheckboxes.attr('required', 'required');
}
});
});
</script>
I have a form that has some simple javascript to validate a simple questionnaire. It used the name attribute on inputs in the form but now the page is integrated with the clients CMS it needs to use a particular naming convention which I can't get to work.
The old form had inputs like this:
<input type="text" name="firstName" />
but with the cms all fields require "fields[firstName]", e.g.:
<input name="fields[first-name]">
How do I get this to work.
Exisiting JS below:
function validate_form ( )
{
valid = true;
if ( ( document.register.question[0].checked == false ) && ( document.register.question[1].checked == false ) && ( document.register.question[2].checked == false ) )
{
alert ( "Please choose an answer" );
valid = false;
}
else if ( document.register.form_title.value == "" )
{
alert ( "Please fill in the 'Title' box." );
valid = false;
}
else if ( document.register.fName.value == "" )
{
alert ( "Please add your First Name" );
valid = false;
}
else if ( document.register.lName.value == "" )
{
alert ( "Please add your Last Name" );
valid = false;
}
else if ( document.register.email.value == "" )
{
alert ( "Please add a valid email address" );
valid = false;
}
else if ( document.register.country.selectedIndex == 0 )
{
alert ( "Please select your Country" );
valid = false;
}
else if ( document.register.dob1.selectedIndex == 0 )
{
alert ( "Please ensure your date of birth is complete" );
valid = false;
}
else if ( document.register.dob2.selectedIndex == 0 )
{
alert ( "Please ensure your date of birth is complete" );
valid = false;
}
else if ( document.register.dob3.selectedIndex == 0 )
{
alert ( "Please ensure your date of birth is complete" );
valid = false;
}
else if ( document.register.terms.checked == false )
{
alert ( "Please check the Terms & Conditions box." );
valid = false;
}
return valid;
}
you can use jquery with something like this:
$("input[name=fields\\[first-name\\]]").val()