#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Javascript Unable to to get value of the property 'focus'. object is null or undefin


    HI guys,

    I am not a pro at JS. But I getting an error, and I am not sure why.
    The same code works in my other functions, but it is not working in a password comparre function. Can someone point me in the right direction as to a fix?

    Thanks



    Function Checkpassword is causing the error when I test the HTML page.


    <script type='text/javascript'>

    function formvalidate(){
    //Set up for the user


    }//Close formvalidate function



    //Function - Checks the Userid is an email address
    function checkUserId(){
    var userCheck = document.getElementById('userid').value;

    if(!userCheck.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/)){
    alert("Error: Incorrect username.");
    setTimeout(function(){document.getElementById.focus('userid')}, 1);
    }else{

    return true;
    }
    }

    //Function - password check
    function checkPassword(password1, passCompare){

    var pass1 = document.getElementById('password1').value;
    var pass2 = document.getElementById('passCompare').value;


    // strip any leading spaces
    pass1 = pass1.replace(/^\s+/g, "");
    pass2 = pass2.replace(/^\s+/g, "");

    // minimum 8 characters
    if ((pass2.length < 8) || (pass2 != pass1) || (pass1 != pass2)) {
    alert ("The two passwords do not match! Try again!");
    //document.register.pass1(setfocus);
    setTimeout(function(){this.getElementById.focus.value('password1')}, 1);
    return false;
    }else {
    return true;
    }
    }
    //Function - To check names are valid entries in form
    function check_FName(element){
    var fname = document.register.fname.value;

    if (!fname.match(/\S/))
    {
    alert('WARNING! Firstname cannot be blank!');

    }
    else if (!fname.match(/^[\w\-\_\+][a-zA-Z]+$/))
    {
    alert('WARNING! Firstname needs to be only letters');
    }
    else
    {
    return true;
    }
    }//close function

    function check_LName(){
    var lname = document.register.lname.value;
    // if (lname.match(/\S/)){
    // return true;
    // }else{
    // alert('Sorry, cannot be blank!');
    // return false;
    // }
    // if(lname.match(/^[a-zA-Z]+$/)){
    // return true;
    // }else{
    // alert('Sorry, Lastname needs to be only letters, and it cannot be blank!');
    // return false;
    // }

    if (!lname.match(/\S/))
    {
    alert('Error: Lastname cannot be blank!');
    }
    else if (!lname.match(/^[a-zA-Z]+$/))
    {
    alert('Error: Lastname needs to be only letters');
    }
    else
    {
    return true;
    }
    }//end function

    //Function - Check City for letter and "-""_"
    function check_Street(){
    var street = document.register.street.value;

    if(street.match(/^[a-zA-Z0-9_-]+$/))
    {
    alert("Error: Correct your street Address.");

    }
    else if(!street.match(/\S/))
    {
    alert("Error: STREET Field cannot be blank");

    }
    else
    {
    return true;
    }


    }//End Function
    function check_city(){
    var city = document.register.city.value;

    //Check for city
    if(!city.match(/^[a-zA-Z-_]+$/))
    {
    alert("Error: You may only use letters for this field or '-''_' inside the city FIELD. ");
    setTimeout(function(){document.getElementById('city').focus();}, 1);
    }
    else if (!city.match(/\S/))
    {
    alert("Error: You may only use letters for this field or '-''_' inside the city FIELD. ");
    }

    else
    {
    return true;
    }
    }//end function

    function check_Province(){

    var province = document.register.province.value;
    if(province =='Please Choose')
    {
    alert("Error: Pleaese choose a province from the list!");
    // document.register.province.focus();
    setTimeout(function(){document.getElementById('province').focus();}, 1);
    }
    else
    return true;
    }


    function check_phone(node){

    var phone = document.getElementById('phone').value;

    //if(document.register.phone.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
    if(!phone.match(/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/))
    {
    alert("Error:Please enster a valid format.(###)###-#### or ###-###-####");
    //document.register.phone.focus();
    setTimeout(function(){document.getElementById('phone').focus();}, 1);
    }
    else if (!phone.match(/\S/))
    {
    alert("Error:Blank ");
    //document.register.phone.focus();
    setTimeout(function(){document.getElementById('phone').focus();}, 1);
    }
    else if (phone.match(/^[(]{0,1}[###][)]{0,1}[###]\-\{0,1}[####]$/))
    {
    alert("Error:Blank ");
    //document.register.phone.focus();
    setTimeout(function(){document.getElementById('phone').focus();}, 1);
    }

    else
    {
    return true;
    }
    }

    function check_country(){

    var country = document.register.country.value;
    if(country =='Please Choose')
    {
    alert("Error: Pleaese choose a province from the list!");
    setTimeout(function(){document.getElementById('country').focus();}, 1);
    return false;
    }
    else
    return true;
    }

    function check_postalcode(postal){

    //var postal = document.register.postal.value;
    postal = document.forms[0].postal.value;

    //if(!postal.match(/^\s*[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z](\s)[-]?\d[a-ceghj-npr-tv-z]\d\s*$/i))
    if(!postal.match(/^\s*[a-zA-Z]\d[a-zA-Z](\s)?\d[a-zA-Z]\d\s*$/i))

    {
    alert("Error: Please enter a valid postal code.");
    document.register.postal.focus();
    return false;
    }

    else if(postal== "" || postal == null || postal.charAt(0) == '' )

    {
    alert("Error: Cannot be blank.");
    document.register.postal.focus();
    return false;
    }
    return true;
    }


    </script>


    <form name='register' onsubmit='return formvalidate()' action="mailto:darrell.locke@unb.ca" method="post">
    <fieldset>
    <legend width="50%">Personal information</legend>
    <p><strong>*</strong>Required Fields</p>

    *User ID : <input type='text' id='userid' onblur='checkUserId()'/><br/>
    *Password: <input type="password" name="password1" id='password1'/><br/>
    *Confirm Password: <input type="password" name="password2" id='passCompare' onChange='checkPassword()'><br/>
    *First Name: <input type="text" name="firstname" id='fname' onChange='check_FName()'><br/>
    *Last Name: <input type="text" name="lname" id='lname' onChange='check_LName()'/><br/>
    *Street: <input type="text" name="street" / id='street' onchange='check_Street()'><br/>
    *City: <input type="text" name="city" id='city' onkeydown="check_city()" onchange="check_city()" /><br/>

    *Province: <select name="province" id='province' onblur='check_Province()' onchange='check_Province()' />
    <option value='Please Choose'>Please Choose</option>
    <option value="bc">British Columbia</option>
    <option value="alta">Alberta</option>
    <option value="manitoba">Manitoba</option>
    <option value="sask">Saskatewan</option>
    <option value="ontario">Ontario</option>
    <option value="quebec">Quebec</option>
    <option value="nb">New Brunswick</option>
    <option value="ns">Nova Scotia</option>
    <option value="pei">Prince Edward Island</option>
    <option value="nf">Newfoundland</option>
    <option value="yukon">Yukon</option>
    <option value="nwt">NWT</option>
    </select><br/>
    *Phone: <input type="text" name="phoneNumber" value='(###)###-####' id='phone' onchange='check_phone()'/> <br/>
    *Country: <select name="country" id='country' onblur='check_country()' onkeydown='check_country()' onchange='check_country()'>
    <option value='Please Choose'>Please Choose</option>
    <option>Canada</option>
    </select><br/>

    PostalCode: <input type="text" name="postalcode" id='postal' onblur='check_postalcode()' onchange='check_postalcode()'/><br/>



    <input type='submit' value='Check Form' /><input type='reset' value'reset'>
    </form>


  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    I got it working.
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo