Javascript 中的 字符串对象 toUpperCase() toString() charAt() indexOf() lastIndexOf() replace() search() substring()

字符串

<!DOCTYPE html>
<html>
  <head>
    <title>js7.html</title>
  </head>
  
  <body>
    <script type="text/javascript">
        var str = "javascript";
        var num = 1234;
        with (document)
        {
            write(str.toUpperCase()+"<br>");
            write(num.toString().charAt(2), "<br>");
            write(str.substring(0,4), "<br>");
        }
    </script>
  </body>
</html>

结果:

JAVASCRIPT
3
java

下面是验证email。如果包含@则认为正确。否则报错。

<!DOCTYPE html>
<html>
  <head>
      <script type="text/javascript">
          function isEmail()
          {
              var email = document.getElementsByName("email")[0].value;
              if (email.indexOf("@") == -1)
              {
                  alert ("请输入正确 email!");
              }
              else
              {
                  alert ("Good!");
              }
          }
      </script>
  </head>
  
  <body>
    <form>
        email: <input type = "text" name = "email">
        <input type = "button" value = "check" onclick = "isEmail();">
    </form>
  </body>
</html>
posted @ 2012-07-27 22:41  allenbackpacker  阅读(489)  评论(0编辑  收藏  举报