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

    Join Date
    Oct 2006
    Posts
    277
    Rep Power
    0

    Jquery regex val problem


    it does not run for email
    Code:
    <script src="jquery-ui-1.10.0.custom/jquery-1.9.1.js"></script>
    <title>form1</title>
    <script language="javascript">
    	$(document).ready(function(){
    		$("input[type='text']").bind("focusout",function(){	
    		var emailRegex = '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';
    		var em	= $("#email").val(); 	
    			var i = $(this).attr("type")+" : "+$(this).val();
    			if ($(this).val()==='' ||  emailRegex.test(em)){
    					alert ("Değer boş yada hatalı");
    				}else {
    					$("#son").html(i)		
    				}
    			})  
    			
    	  });
    </script>
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="">
      <input type="text" name="name" id="name" />
      <input type="text" name="pass" id="pass" />
      <input type="text" name="email" id="email" />
      <input type="submit" name="button" id="button" value="Submit" />
    </form>
    
    <div id="son"></div>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    I've taken a stab at getting this working: http://jsfiddle.net/Fkm42/

    I noticed that your RegExp was defined with single quotes instead of forward slashes causing the call test() to throw an error. Also I replaced your regex with one from this page, added a not sign (!) to emailRegex.test(em) so that the message box will be displayed if the email address didn't match the regex, and added a few missing semicolons.
    Code:
    $(document).ready(function () {
        $("input[type='text']").bind("focusout", function () {
            var emailRegex = /^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/;
            var em = $("#email").val();
            var i = $(this).attr("type") + " : " + $(this).val();
            if ($(this).val() === '' || !emailRegex.test(em)) {
                    alert("Değer boş yada hatalı");
                } else {
                    $("#son").html(i);
                }
        });
    });

IMN logo majestic logo threadwatch logo seochat tools logo