Нужен простой js

Discussion in 'PHP' started by brasco2k, 14 Mar 2008.

  1. brasco2k

    brasco2k Elder - Старейшина

    Joined:
    23 Nov 2007
    Messages:
    258
    Likes Received:
    91
    Reputations:
    0
    Вобщем есть поле для ввода email:
    <input type='text' name='email' >

    Нужно написать js который проверяет правильность ввода т.е [email protected]
     
  2. FeraS

    FeraS Elder - Старейшина

    Joined:
    19 Jan 2007
    Messages:
    555
    Likes Received:
    420
    Reputations:
    76
    http://www.mexxs.net/reviews.php?file=25
    http://web-utils.net/javascript/validate-email-form
     
  3. kolka

    kolka New Member

    Joined:
    19 Feb 2008
    Messages:
    4
    Likes Received:
    4
    Reputations:
    0
    Code:
      if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(f.email.value))) {
           alert('Введите правильный е-mail!');f.email.select();
           return false;
      }
     
    1 person likes this.
  4. astrologer

    astrologer Elder - Старейшина

    Joined:
    30 Aug 2007
    Messages:
    837
    Likes Received:
    267
    Reputations:
    59
    kolka,
    Code:
    javascript:alert( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test('_@_.__') )
    // Выведет: true
    Code:
    javascript:alert( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test('[email protected]') )
    // Выведет: false
     
  5. brasco2k

    brasco2k Elder - Старейшина

    Joined:
    23 Nov 2007
    Messages:
    258
    Likes Received:
    91
    Reputations:
    0
    подскажите плз с php , ато чото не один js не пашет.....
     
  6. astrologer

    astrologer Elder - Старейшина

    Joined:
    30 Aug 2007
    Messages:
    837
    Likes Received:
    267
    Reputations:
    59
    А как ты его применяешь?
     
  7. brasco2k

    brasco2k Elder - Старейшина

    Joined:
    23 Nov 2007
    Messages:
    258
    Likes Received:
    91
    Reputations:
    0
    Вобщем есть форма

    PHP:
    <form method='post' onSubmit "return checkform(this); return isEmailCorrect(this,'email')">
    E-mail:<input type='text' name='email'  required>
    перед ней код:
    <script language='JavaScript' src="../js/reg.js"></script>

    в нем:

    function isEmailCorrect(formname, fieldname) {
    var re = /^[a-zA-Z0-9\-_](([a-z0-9\-_\.\+\&]?)+[a-z0-9])?\@((\w([a-zA-Z0-9\-_]+\w)?\.[a-z]{2,4})|(([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d |2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5]))|(localhost))$/i;
    if(re.test(eval("document.forms['"+formname+"']."+fieldname+".value"))) return true;
    else {
    alert("Email incorrect!");
    eval("document.forms['"+formname+"']."+fieldname+".focus()");
    return false;
    }
    }

    и траляля...
     
    #7 brasco2k, 14 Mar 2008
    Last edited: 14 Mar 2008
  8. FeraS

    FeraS Elder - Старейшина

    Joined:
    19 Jan 2007
    Messages:
    555
    Likes Received:
    420
    Reputations:
    76
    HTML:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    
    <body>
    
    
    <script language="javascript">
    function isEmailCorrect(formname, fieldname) {
      var re = /^[a-zA-Z0-9\-_](([a-z0-9\-_\.\+\&]?)+[a-z0-9])?\@((\w([a-zA-Z0-9\-_]+\w)?\.[a-z]{2,4})|(([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d |2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5]))|(localhost))$/i;
      if(re.test(eval("document.forms['"+formname+"']."+fieldname+".value")))
        return true;
      else {
        alert("Email incorrect!");
        eval("document.forms['"+formname+"']."+fieldname+".focus()");
        return false;
      }
    }
    </script>
    <form action="" name="contactForm" onsubmit="return isEmailCorrect('contactForm', 'email')">
    Name: <input type="text" name="name"> &nbsp;
    Email: <input type="text" name="email">
    <input type="submit" value="OK">
    </form>
    
    
    </body>
    </html>
    
    все робит
     
  9. brasco2k

    brasco2k Elder - Старейшина

    Joined:
    23 Nov 2007
    Messages:
    258
    Likes Received:
    91
    Reputations:
    0
    Спасибо, а как поместить в форму 2 запроса:

    1. onsubmit="return isEmailCorrect('contactForm', 'email')"

    2.onsubmit="return checkform(this)"

    ?
     
  10. astrologer

    astrologer Elder - Старейшина

    Joined:
    30 Aug 2007
    Messages:
    837
    Likes Received:
    267
    Reputations:
    59
    Если не заморачиваться, то так:
    Code:
    onsubmit="return (isEmailCorrect('contactForm', 'email') && checkform(this))"
    Но разве функция checkform по логике не должна заодно проверять и корректность введённых данных?
     
  11. brasco2k

    brasco2k Elder - Старейшина

    Joined:
    23 Nov 2007
    Messages:
    258
    Likes Received:
    91
    Reputations:
    0
    Ну у меня просто там кое что другое она проверяяет)
     
  12. Phm.phx

    Phm.phx Banned

    Joined:
    12 Jan 2008
    Messages:
    73
    Likes Received:
    232
    Reputations:
    1
    PHP:
    /* Проверяет является ли строка адресом e-mail
    */
    function strings_isemail($string)
    {
    return 
    preg_match('%[-\.\w]+@[-\w]+(?:\.[-\w]+)+%'$string);
    }
     
  13. Painkiller

    Painkiller New Member

    Joined:
    4 May 2007
    Messages:
    4
    Likes Received:
    0
    Reputations:
    0
    if(!preg_match("|[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+\.[a-z0-9]|",$mail))
    { $back = "Неправильно заполнена форма!"; $_SESSION['wrong'] = $back;
    echo "<html><head><meta http-equiv='Refresh' content='0 url=http://localhost/admin/'>
    </head></html>";
    die(); }
     
  14. NOmeR1

    NOmeR1 Everybody lies

    Joined:
    2 Jun 2006
    Messages:
    1,068
    Likes Received:
    783
    Reputations:
    213
    На JS нет никакого смысла проверять, т.к. можно обойти.
    Вот чОткая проверка
    PHP:
    <?php
        $email 
    '[email protected]_rulez.com';
        if(
    preg_match('~^[A-Za-z0-9_\.-]+@[A-Za-z0-9_\.-]+\.[a-zA-Z]+$~'$email)) {
            echo 
    'Yeaah. This mail is corrrect!';
        } else {
            echo 
    'Ohhh... Are you crazy??';
        }
    ?>
     
    1 person likes this.
  15. banned

    banned Banned

    Joined:
    20 Nov 2006
    Messages:
    3,324
    Likes Received:
    1,193
    Reputations:
    252
    PHP:
    <?php
    if(preg_match('~^[A-Za-z0-9_\.-]+@[A-Za-z0-9_\.-]+\.[a-zA-Z]{2,6}$~'$email)) ....
    ?>
    А то адрес, [email protected] и [email protected] будут валидны +)