Results 1 to 3 of 3

Thread: Adding Javascript to a PHP page not working...

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding Javascript to a PHP page not working...

    Could someone please tell me where about to put this code:
    Code:
    <form name="validation" onSubmit="return checkbae()">
    Please input a valid email address:<br>
    <input type="text" size=18 name="emailcheck">
    <input type="submit" value="Submit">
    </form>
    <script language="JavaScript1.2">
    
    var testresults
    function checkemail(){
    var str=document.validation.emailcheck.value
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
    testresults=true
    else{
    alert("Please input a valid email address!")
    testresults=false
    }
    return (testresults)
    }
    </script>
    
    <script>
    function checkbae(){
    if (document.layers||document.getElementById||document.all)
    return checkemail()
    else
    return true
    }
    </script>
    in this file? It's a php, just converted to .txt so you all can see what's going on... http://www.curiousclothing.com/index.txt

  2. #2
    Join Date
    May 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    checkbae? Bu then again, I don't know very much about JScript.

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by tomyknoker
    Could someone please tell me where about to put this code:
    To be quite honest, the bin would be the best place (you should be happy about not having wrote it).

    First, the alternative. Place the following in the head element (just before </head> will do):

    HTML Code:
    <script type="text/javascript">
      function validate(form) {
        if (!isEmail(form.elements.emailcheck.value)) {
          alert('Please enter a valid e-mail address.');
          return false;
        }
        return true;
      }
    
      function isEmail(value) {
        return /[^@]+@[^@.]+(\.[^@.]+)+/.test(value);
      }
    </script>
    The form itself should look something like:

    HTML Code:
    <form action="..." onsubmit="return validate(this);">
      <div>
        <label>E-mail address: <input name="emailcheck" value=""></label>
        <input type="submit" value="Submit">
      </div>
    </form>
    Now, for reasons to avoid what you posted.

    <script language="JavaScript1.2">
    The language attribute has long been deprecated in favour of the type attribute. Worse still, you might actually get what you ask for: JavaScript v1.2. That particular version was very much different to versions before and since, and to actually have a script evaluated according to its rules could be quite a shock.

    var testresults
    function checkemail(){
    That variable declaration makes testresults a global variable. That shouldn't be at all desirable - there's need for it to be global - and little things like this brings the knowledge of the original author into doubt.

    var str=document.validation.emailcheck.value
    Form elements should be accessed using the forms collection, and controls within that form should be accessed using the elements collection of that form. Alternatively, one can do what I have above, and obtain a direct reference to the form within the event handler using the this operator. In any case, using the shortcut of access through the document object is a bad idea; that shortcut should never have been exposed by browsers in the first place.

    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    This regular expression is too restrictive and will reject valid e-mail addresses unnecessarily. Moreover, it uses non-capturing parentheses [&#160;(?:&#160;...&#160&#160;] for no apparent reason, which may cause syntax errors in older browsers. This is also quite perverse when one considers that the requested JavaScript version is 1.2; it never featured non-capturing parentheses.

    If you're familiar with Perl-compatible regular expressions, you might wonder why my replacement is so simplistic (though, technically, even it will reject some valid addresses - e-mail addresses don't have to contain a dot in the right-hand side). The answer is simple: it's not possible to validate an e-mail address from the client. The only way to ensure that an address is valid is to attempt to send mail to it, and have the addressee do something upon receipt (send a reply or go to a unique confirmation URL). Sure, one can write a thorough syntactic check, but that doesn't prevent an authentic-looking address from being used.

    <script>
    The type attribute is required.

    function checkbae(){
    if (document.layers||document.getElementById||document.all)
    This is, more-or-less, browser detection by object inference. Any time you see something like it, it's almost a sure bet that the author doesn't know what they're doing. Be careful, though: feature detection (sometimes referred to as object detection), which can look similar, but is usually more elaborate, shows quite the opposite - that the author is a good one.

    Hope that helps,
    Mike

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
  •