JavaScript From Question.

by 17 replies
20


<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>



<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>

#programming #javascript #question
  • I suggest using Stackoverflow.com for this kind of question, its the bomb.
    • [1] reply
    • 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>
      • [1] reply
  • Instead of using javascript you should use server side script validation according to me its the best way to do it
    • [1] reply
    • Thanks for the suggestion seowonder56.

Next Topics on Trending Feed

  • 20

    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.