Closed Thread
Thread Tools Search this Thread
Unread 16th Oct 2012, 11:02 PM   #1
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Hi Warriors!

I am trying to validate a JS form without getting the alert to show up as a pop up on the page. I want the alert to be displayed inline with the webpage. I believe we need to use 'innerHtml' syntax but I am not sure how.

Also this is there anyway to make the form accept the coupon code in lower case?

Here's my code:


<html>
<head>
<title>Coupon code</title>
<script type="text/javascript">
function isValid(){ var coupon = document.getElementById('coupon').value;
if (coupon == "25OFF")
{alert('Coupon code has been applied.')}
else {alert('Invalid coupon code')} }
</script>
</head>
<body>
<form name="couponField" action="">
Coupon code: <input type="coupon" id="coupon" name="coupon"> <input type="button" value="Apply Coupon Code" onclick="isValid();"> </form> </body> </html>

Below is the code for the effect (the sliding button) that I am trying to achieve:

<html>
<head>
<title>Coupon code</title>
<script type='text/javascript'>
function notEmpty(elem, helperMsg){
var valid = false; if(elem.value.length == 0){ document.getElementById('errorMessage').innerHTML = helperMsg; elem.focus(); }
else{
valid = true; }
return valid; }
</script>
</head>
<body>
<form action='something.php' method='POST' onsubmit="return notEmpty(document.getElementById('coup'), 'Please Enter a Valid Coupon Number')">
Coupon Code:&nbsp;<input TYPE = "text" NAME = "coupon field" id='coup' SIZE = "20" VALUE = ""/>
<span id='errorMessage'></span>
<input type="submit" value="Apply coupon code"/> </form>
</body>
</html>

Thanks in advance for your help!
a7521 is offline  
Unread 18th Oct 2012, 08:12 PM   #2
New Warrior Member
 
Join Date: 2006
Location: , , USA.
Posts: 13
Thanks: 0
Thanked 1 Time in 1 Post
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

I suggest using Stackoverflow.com for this kind of question, its the bomb.
dutrowllc is offline  
Unread 19th Oct 2012, 04:59 AM   #3
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

try this, make sure you use it on server side

<html>
<head>
<title>Coupon code</title>
<style>
#error{
color: red;
font-weight: bold;
}
</style>
</head>

<body>
<form name="couponField" action="">
<span id="error"></span>
Coupon code:
<input type="coupon" id="coupon" name="coupon">
<input type="button" id="submit" value="Apply Coupon Code">
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$('#submit').click(function(){
if($('#coupon').val() == '25OFF'){
$('#error').text('correct code!');
}else{
$('#error').text('wrong code!');
}

});
</script>

</body>
</html>
viescripts is offline  
Unread 19th Oct 2012, 10:29 AM   #4
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

@dutrowllc thanks. I will check Stackoverflow out.
@viescripts the script doesn't seem to run. I also tried copying and pasting the script section of the code between the <head> </head> tags but no luck. I am using Firefox.
a7521 is offline  
Unread 20th Oct 2012, 03:50 AM   #5
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

copy it to your webhosting, see the same uploaded here:
http : / / jpesafelists dot com/victor/19/
viescripts is offline  
The Following User Says Thank You to viescripts For This Useful Post:
Unread 20th Oct 2012, 12:40 PM   #6
Web Developers
 
Sitesupplier's Avatar
 
Join Date: 2012
Posts: 22
Thanks: 1
Thanked 5 Times in 5 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Don't use innerHTML, use textContent. Manipulating innerHTML is for manipulating the HTML, not just text. However, if you want an HTML-formatted message, go right ahead.

I've made a little sample for you on JSFiddle, feel free to play with it.

Edit this Fiddle - jsFiddle

Sitesupplier is offline  
Unread 20th Oct 2012, 09:30 PM   #7
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

@viescripts ....thanks for the code! It is exactly what we were looking for. We appreciate it much.
a7521 is offline  
Unread 20th Oct 2012, 09:38 PM   #8
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

@sitesupplier...thanks for taking the time to write the code. Also thanks for the helpful tip about the textcontent syntax. The code runs perfectly fine on JSfiddle. However if we copy and save it to a notepad as an HTML file it does not execute the error message. I have copied the JS and the CSS part of the code in the <head> section. I also tried - jsFiddle demo and then copied and pasted the code exactly like it is by doing 'View Source'. However in both cases no error message is displayed. Is there any other way for me to test the code? Thanks again for taking the time and helping us out.
a7521 is offline  
Unread 21st Oct 2012, 12:57 AM   #9
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Originally Posted by a7521 View Post

@viescripts ....thanks for the code! It is exactly what we were looking for. We appreciate it much.
btw I found the problem why it was not working locally, google gave a wrong link to jquery library:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>


they forgot the http://.


Find the piece of code above and swap it with this one:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>


on your local machine should work too as JavaScript is interpreted by browser.
viescripts is offline  
Unread 21st Oct 2012, 01:31 AM   #10
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Thanks for the code snippet viescripts. Is it possible to remove validation if the user leaves the field empty? This is a field for a coupon code input. So some users may have the code and some won't. The ones that won't will leave the field empty. We still want them to go ahead and submit their order. On the other hand the ones that do will enter the promo code - 25OFF to get a 25% discount on their order. When they do the code 25OFF will be validated for an exact match. Can this be done? Thanks again for your help!
a7521 is offline  
Unread 22nd Oct 2012, 03:21 AM   #11
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Originally Posted by a7521 View Post

Thanks for the code snippet viescripts. Is it possible to remove validation if the user leaves the field empty? This is a field for a coupon code input. So some users may have the code and some won't. The ones that won't will leave the field empty. We still want them to go ahead and submit their order. On the other hand the ones that do will enter the promo code - 25OFF to get a 25% discount on their order. When they do the code 25OFF will be validated for an exact match. Can this be done? Thanks again for your help!
This is just a example of how it would work.
The button doesn't submit the form, it's just a button.

If you need to check the coupon coe, you might want to do that on the result page (e.g. after form submission). This is usually checked by php or any other language.

JavaScript is not ok for such a thing.
viescripts is offline  
Unread 23rd Oct 2012, 12:00 AM   #12
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Viescripts,
Thanks for the reply. We do have a php script in place that does the actual submitting and applying of the discount code. I was referring to your code (@ 10-19-2012, 05:59 AM). It validates the discount code '25OFF', so if the user enters anything other than 25OFF it gives an error message. However it also gives an error message if the field is left blank. Is there any way to remove the validation for the empty field but still keep the validation for the '25OFF' discount code? So the field will return an error message if the user types anything other than '25OFF' but will not give an error if the field is left blank. Thanks again.
a7521 is offline  
Unread 23rd Oct 2012, 04:42 AM   #13
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

modified java script version:

$('#submit').click(function(){
if($('#coupon').val() == '25OFF'){
$('#error').text('correct code!');
}else if($('#coupon').val() == ''){
$('#error').text('form submitted, with no coupon discount code!');
}else{
$('#error').text('wrong code!');
}
viescripts is offline  
Unread 24th Oct 2012, 04:03 AM   #14
Web Developers
 
Sitesupplier's Avatar
 
Join Date: 2012
Posts: 22
Thanks: 1
Thanked 5 Times in 5 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Are you still looking for help with this problem? It doesn't sound too complex, but I concur that JavaScript is not really the most appropriate language for this. Granted, it would seem to be, but you're just doubling up on your code and error checking since you have to write it in two languages.

The discount coupon should be checked and validated in PHP, primarily. Since JavaScript can be disabled, your code can be rendered useless and there's nothing you can do about it. It should be used as a secondary check or simply for the sake of completeness and user-friendliness, not for anything else.

Once your POST has been made, you can just call the necessary PHP script with the "action" property on the form element, for example:

Code:
<form method="post" action="path_to_php_file.php">
    <!-- form elements -->
</form>

Sitesupplier is offline  
Unread 25th Oct 2012, 06:59 AM   #15
Active Warrior
 
seowonder56's Avatar
 
Join Date: 2012
Posts: 38
Thanks: 0
Thanked 4 Times in 4 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Instead of using javascript you should use server side script validation according to me its the best way to do it
seowonder56 is offline  
Unread 25th Oct 2012, 11:46 PM   #16
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Thanks for the modified code Viescripts. Appreciate it!
a7521 is offline  
Unread 25th Oct 2012, 11:51 PM   #17
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Thanks for the suggestion Sitesupplier. PHP will certainly be a better way to go. Also with JS there's a chance that the user may see our coupon code which can be avoided if we use PHP.
a7521 is offline  
Unread 25th Oct 2012, 11:52 PM   #18
Warrior Member
War Room Member
 
Join Date: 2011
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default
Re: JavaScript From Question.
Share on: 
fb share twitter share gplus share more share

Thanks for the suggestion seowonder56.
a7521 is offline  
Closed Thread


Bookmarks

Tags
javascript, question

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 04:30 PM.