10 Replies - 688 Views - Last Post: 31 January 2013 - 06:08 AM

#1 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

javascript validation

Posted 29 January 2013 - 04:08 AM

Hi guys I need to use Javascript to validate a field whereby it should count how many digits it contains. It should contain a maximum of 16 digits. If it exceeds or is less than 16 then it should display a message however nothing is happening. This is what I have tried so far:

<script type="text/javascript">
	function validateForm()
{
var x=document.forms["checkout"]["num"].value;
	if (pw.length < 16){
    alert("Your credit card must be 16 digits long.);
    return false;
}
	</script>




Is This A Good Question/Topic? 0
  • +

Replies To: javascript validation

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: javascript validation

Posted 29 January 2013 - 04:15 AM

Well, I believe you should change the line 5 to

if (x.length < 16){
 alert("Your credit card must be 16 digits long.");//Added the "


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Re: javascript validation

Posted 29 January 2013 - 04:27 AM

Unfortunately still no luck, this is my whole page:


<?php
// Redirect if this page was accessed directly: 
if (!defined('BASE_URL')) { 

    // Need the BASE_URL, defined in the config file: 
    require_once ('../includes/config.inc.php'); 
     
    // Redirect to the index page: 
    $url = BASE_URL . 'index.php'; 
    header ("Location: $url"); 
    exit; 
     
} // End of defined() IF. 

?> 
<?php 
if(!isset($_SESSION['username'])) {
echo "Welcome Guest!"; 
} else {
echo "<font color='red'>You are logged in as </font><font color='purple'>$_SESSION[username].</font><font color='orange'><a href='index.php?p=logout'>  logout?</a> </font><br><font color='red'>Dont forget $_SESSION[username]. always feel free to contact us for support when needed most</font><br><br>";
}
?>
<h2>Checkout</h2> 
<?php  
include"config2.php";
        $total = $_GET['total']; //this total variable will read the url for the total sum of the products. 
$user=$_SESSION['username']; //using a session to find the user's username.
//select shipping address of the user to allow the user to decide whether the address which they registered with is still correct.	
        $result = mysql_query("SELECT * FROM users WHERE username='".$user."'"); 
        $row = mysql_fetch_array($result); 
         
        echo
                '<p> Your shipping address is: <br /> 
				<br>
                <b>'.$row['address'].'<br /> 
				</br>
                </b> 
                </p> 
         
        <p>The total payable amount for this purchase is <b>&pound;'.$total.'</P>'; ?> 
	<!--  the following is a form which is used for the user to enter their payment information.   -->
	<script type="text/javascript">
	function validateForm()
{
var x=document.forms["checkout"]["num"].value;
	if (x.length < 16){
    alert("Your credit card must be 16 digits long.");
    return false;
}
	</script>
<table width="95%" border="0" cellspacing="2" cellpadding="2"> 
  <tr> 
    <td height="100" valign="top" class="paddingLeft"><form action="index.php"  method="GET" name="checkout" id="checkout" onsubmit="return validateForm()"> 
        <p> 
          <input id="p" name="p" type="hidden" value="conf"/> 
          <?php echo '<input id="amount" name="amount" type="hidden" value="'.$total.'"/>';  ?> Card Number</br> 
          <input type="text" name="num" size="30" maxlength="35" placeholder="Card Number" /> 
          <br /> 
          Name on the Card</br> 
          <input type="text" name="bearer" size="30" maxlength="35" placeholder="Name on the Card" /> 
          <br /> 
          Valid From (mmyy)</br> 
          <input type="text" name="syear" size="4" maxlength="4" /> 
          <br /> 
          Valid Till (mmyy)</br> 
          <input type="text" name="fyear" size="4" maxlength="4" /> 
          <br /> 
          Card Type</br> 
          <input type="text" name="type" size="30" maxlength="35" placeholder="Card Type" /> 
          <br /> 
          <input type="submit" value="Proceed" /> 
          <br /> 
      </form></td> 
  </tr> 
</table> 
<p><a href="index.php?p=products">Do you want to continue shopping?</a></p>
<img src="images/checkout.jpg" alt="cart" id="cart" width="80" height="80">



Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: javascript validation

Posted 29 January 2013 - 04:31 AM

Well, try this and see...
var x=parseInt(document.checkout.num.value);


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Re: javascript validation

Posted 29 January 2013 - 04:36 AM

Still no luck, i am probably returning the form validation wrong.
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: javascript validation

Posted 29 January 2013 - 04:58 AM

I've moved this thread to the Javascript forum.


The main problem with that code is that it's a complete mess. You've got PHP, HTML, Javascript and SQL code all mixed up in one file, most of which isn't so much as indented in any sane manner. It's small wonder you're having trouble spotting the error in all that. - It's important to keep things organized: put Javascript and CSS code in external files, and try your best to keep the PHP and HTML separate as well. Use templates, or at least be consistent about how you generate the HTML. If you have large blocks of HTML code being printed as strings from PHP, it's a good bet that your code needs some tidying up. - See this tutorial for some tips on how to do that.

As for the problem itself, let me show you a re-formatted version of your Javascript code. It's the exact same thing you posted in your original post, just with a proper indentation style applied:
function validateForm() {
    var x = document.forms["checkout"]["num"].value;
    if (x.length < 16) {
        alert("Your credit card must be 16 digits long.");
        return false;
}


Can you spot the problem when it's set up like that?
(Hint: try matching up the { and } brackets)
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: javascript validation

Posted 29 January 2013 - 05:03 AM

You are missing a closing bracket } for your if statement - your Console would have told you this!
And, per your request, you want to check that the number of characters is not equal to 16:

    <script type="text/javascript">
       	function validateForm() {
            var x=document.forms["checkout"]["num"].value;
            if (x.length != 16) {
                alert("Your credit card must be 16 digits long.");
                return false;
            }
            return true;
        } 
    </script>

BTW It is preferred that you give your credit-card field an ID and use this in preference to the name-attribute when using JS.

This post has been edited by andrewsw: 29 January 2013 - 05:04 AM

Was This Post Helpful? 1
  • +
  • -

#8 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: javascript validation

Posted 29 January 2013 - 05:32 AM

Hi, I am also learning javascript but not good so far but i think this code can help you
function validateForm()
		{
			 
			var x=ocument.forms["checkout"]["num"].value;
			
                                                if(!isNaN(x))
			{
				if( x.length!=16)
				{
				alert("Your credit card must be 16 digits long And you enter "+x.length);
				return;
				}
			}
			else alert("Enter Valid Number");
		}


Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,244
  • Joined: 12-December 12

Re: javascript validation

Posted 29 January 2013 - 06:09 AM

@Vishu
You've dropped the d of 'document' and the validation function needs to explicitly return false; in order to prevent the form submission.

If you want to use isNaN then I would first use x = parseInt(x); and then check this value.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: javascript validation

Posted 29 January 2013 - 08:29 AM

tip: if you set the maxlength attribute of a text field, you cannot enter more characters than the specified length.
Was This Post Helpful? 0
  • +
  • -

#11 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Re: javascript validation

Posted 31 January 2013 - 06:08 AM

Thanks for all your help guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1