Dark Knight
Dark Knight

Reputation: 137

How do I validate my html form controls using javascript in cakephp?

I am using a popup and applying form inside it. Now I want my controls to be validated using javascript. But whenever I try to submit my form it does not validate my form fields at all and simply submits the page

My js code is given below :

function validateForm()
 {  
    var regex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    var uname = document.getElementById('user_name').value;
    if(uname=="")
    {
        alert("You cannot leave the email field empty");
        return false;
    }
    else if(uname.search(regex) == -1)
    {
        alert("Invalid email address format");
        return false;
    }
    else
    {
        return true;
    }
    return true;

 }

My HTML div code is given below . I have used div because my popup is loaded from that div only :

<div id="logincontent">
<div class="form-box-top">
    <div class="form-box-bottom">
    <?php echo $this->Form->create('User',array('controller' => 'users', 'action' => 'login', 'id'=>'login', 'name'=>'login','onsubmit'=>'return validateForm();'));?>
        <div class="form-box-middle">
            <div class="login-form">
                <div class="re-title3">
                    <a class="pass" onclick="forget();" href="#">forgot password</a>
                </div>
                <div class="input-row">
                    <label>EMAIL</label>                    
                    <?php echo $this->Form->input('username', array('label' => '','class'=>'required','style'=>'margin: -5px 0px 0px 24px;','id'=>'user_name')); ?>
                </div>
                <div class="input-row"> 
                    <label>PASSWORD</label>
                    <?php echo $this->Form->input('password', array('type'=>'password','class'=>'required','label' => '','style'=>'margin: -5px 0px 0px 24px;')); ?>
                </div>
            </div>
            <div class="button_3">
                <?php echo $this->Form->submit('',array('label' => '','class'=>''));?>
            </div>
            <div class="button_3_1">
            <a style="background: url(<?php echo $this->webroot       ?>/images/registor-bt2.png) no-repeat left top;
float: left;border: 0px;width: 236px;height: 47px;" onclick="register()"; href="#"></a>
            </div>
            <div class="cr"></div>
        </div>
    <?php echo $this->Form->end(); ?>
    </div>
</div>

Now whenever I submit my form I want my fields to be validated by js function

I have tried lot but dont know it gives malicious results or unpredicted results.

Any suggestion will be entertained and appreciated..

Thanx in advance....

Upvotes: 0

Views: 1309

Answers (1)

Igor L.
Igor L.

Reputation: 3465

Install firebug, if you don't use it already.

I would recommend using a JQuery UI dialog -> modal form & validation

If you dont want to use it, you may at least want to check out the regular expression code provided on line 16.

Upvotes: 1

Related Questions