SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb [Code Review] Help in inproving code

    Can you guys tell me how I could inprove this code
    Code JavaScript:
    NOnumbershere=new RegExp(/\d/);
    NOsymbolshere=new RegExp(/[+.,*!"$%&()= ?^`;:_-]/);
    emailpattern=new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
    passwordpattern=new RegExp(/(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/);
    warnicon = '<img align="left" src="images/warn.gif"/>';
    function active_field(event)
    {
    event.className = "processing";
    }
    function check__regular_text_field(event)
    {
    errorid = event.id + "error";
    error = document.getElementById(errorid);
    thefield=event;
    thevalue=thefield.value;
    thevaluetolower=thevalue.toLowerCase();
    firstchartoupper = thevaluetolower.charAt(0).toUpperCase() + thevaluetolower.slice(1);
    thevalueint = parseInt(thevalue.length);
    if (NOnumbershere.test(thevaluetolower) == true && NOsymbolshere.test(thevaluetolower) == true)
    {
    thefield.className = "incorrect_field";
    error.innerHTML = '<?php echo $no_numbers_or_special_chars_error; ?>';
    }
    else if (NOnumbershere.test(thevaluetolower) == true)
    {
    thefield.className = "incorrect_field";
    error.innerHTML = warnicon + '<?php echo $no_numbers_error; ?>';
    }
    else if (NOsymbolshere.test(thevaluetolower) == true)
    {
    thefield.className = "incorrect_field";
    error.innerHTML = warnicon + '<?php echo $no_special_chars_error; ?>';
    }
    else if (thevalueint <= 1)
    {
    thefield.className = "incorrect_field";
    error.innerHTML = warnicon + '<?php echo $not_enough_chars_error; ?>';
    }
    else
    {
    thefield.className = "correct_field";
    thefield.value = firstchartoupper;
    error.innerHTML = "";
    }
    }
    function check__email_text_field(event)
    {
    errorid = event.id + "error";
    error = document.getElementById(errorid);
    thefield = event;
    thevalue = thefield.value;
    thevaluetolower = thevalue.toLowerCase();
    var x = document.getElementById("email");
    var y = document.getElementById("emailc");
    thevalueint = parseInt(thevalue.length);
    thevalueintx = parseInt(x.value.length);
    thevalueinty = parseInt(y.value.length);
    if (thevalueint <= 6)
    {
    thefield.className="incorrect_field";
    error.innerHTML = warnicon + '<?php echo $not_enough_chars_2_error; ?>';
    }
    else if (emailpattern.test(thevaluetolower) == false)
    {
    thefield.className="incorrect_field";
    error.innerHTML = warnicon + '<?php echo $incorrect_email_format_error; ?>';
    }
    else
    {
    thefield.className = "correct_field";
    thefield.value = thevaluetolower;
    error.innerHTML = '';
    }
    if (x.value == "" || y.value == "")
    {
    x.className = "processing";
    y.className = "processing";
    }
    else if (x.value == y.value)
    {
    x.className = "correct_field";
    y.className = "correct_field";
    }
    else if (x.value != y.value && thevalueinty >= thevalueintx )
    {
    x.className = "incorrect_field";
    y.className = "incorrect_field";
    error.innerHTML = warnicon + '<?php echo $incorrect_email_match_error; ?>';
    }
    }
    function check__password_text_field(event)
    {
    errorid = event.id + "error";
    error = document.getElementById(errorid);
    thefield = event;
    thevalue = thefield.value;
    var x = document.getElementById("password");
    var y = document.getElementById("passwordc");
    thevalueint = parseInt(thevalue.length);
    thevalueintx = parseInt(x.value.length);
    thevalueinty = parseInt(y.value.length);
    if (thevalueint <= 7)
    {
    thefield.className="incorrect_field";
    error.innerHTML = warnicon + '<?php echo $not_enough_chars_3_error; ?>';
    }
    else if (passwordpattern.test(thevalue) == false)
    {
    thefield.className = "incorrect_field";
    error.innerHTML = warnicon + '<?php echo $incorrect_password_format_error; ?>';
    }
    else
    {
    thefield.className = "correct_field";
    error.innerHTML = '';
    }
    if (x.value == "" || y.value == "")
    {
    }
    else if (x.value == y.value)
    {
    x.className = "correct_field";
    y.className = "correct_field";
    }
    else if (x.value != y.value)
    {
    x.className = "incorrect_field";
    y.className = "incorrect_field";
    error.innerHTML = warnicon + '<?php echo $incorrect_password_match_error; ?>';
    }
    }

    Here is a link to the working code

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Please see this post for my code review for your other post that contains nearly identical code.

    You can easily apply the information from that post to your above code.
    Last edited by paul_wilkins; May 15, 2011 at 17:11.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •