SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery Validation and Alphanumeric Additional Methods

    OK, I'm trying to get the alphanumeric/additional methods working with the validation plugin, but so far, I can't make it fire. I've read through the plugin docs and multiple example on Stack and it just isn't doing the validation. It's not throwing any errors either. Anyone see what I'm missing?

    here's the JS:
    Code:
    <script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="scripts/jquery-validation/jquery.validate.min.js"></script>
    <script type="text/javascript" src="scripts/jquery-validation/additional-methods.min.js"></script>
    <script type="text/javascript">
    
    	$(document).ready(function(){
    
    		var validator=$("#registration").validate({
    	        rules: {
    	            "user_name": {
    	                required: true,
    	                alphanumeric: true,
    	                rangelength: [7, 15]
    
    	            }
    	        },
    	        messages: {
    	            "user_name": {
    	                required: "You must enter a Username",
    	                alphanumeric: "Login format not valid",
    	                rangelength: "Username improper length"
    	            }
    	        }
    	    });
    	});
    	</script>
    form bit:
    Code:
    <div><label for="user_name">User Name</label>
    <input name="user_name" id="user_name" type="text" maxlength="15" /></div>

  2. #2
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've even tried this with no luck either
    Code:
    <script type="text/javascript">
    
    	$.validator.addMethod("loginRegex", function(value, element) {
            return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
        }, "Username must contain only letters, numbers, or dashes.");
    
    	$(document).ready(function(){
    
    		$("#registration").validate({
    	        rules: {
    	            "user_name": {
    	                required: true,
    	                rangelength: [7, 15],
    	                loginRegex:true
    
    	            },
    	            "user_pw2":{
    	            	required: true,
    	            	equaltTo: "#user_password",
    
    	            },
    	            "user_email":{
    	            	required: true,
    	            	email: true
    	            }
    	        },
    	        messages: {
    	            "user_name": {
    	                required: "You must enter a Username",
    	                rangelength: "Username improper length",
                    	loginRegex: "Login format not valid"
    	            },
    	            "user_pw2":{
    	            	required:"Repeat your password",
    	            	equaltTo:"Enter same password as above"
    	            },
    	            "user_email":{
    	            	required: "You must enter an email address",
    	            	email: "Improper email format"
    	            }
    	        }
    	    });
    	});
    	</script>

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,715
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Your first code example works for me. Have you checked that the scripts are accessible?

    If you link to a test page that demonstrates the problem, we can find out what else is causing the problem.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    It looks like you want a 7-15 character alpha-numeric string.

    You can do that without using some plug-in or jquery.


    Code JavaScript:
            <form action="" method="post">
                <input type="text" name="txtUsername" id="txtUsername" maxlength="15" onblur="testThis(this.value)" />
            </form>
     
           <script type="text/javascript">
                           function testThis(val){
                    var str = (/^[\da-z]{7,15}$/.test(val.toLowerCase()))? 'valid' : 'invalid';
                    alert('username is '+ str);
                }
            </script>

    The onblur is just for testing purposes.

  5. #5
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Your first code example works for me. Have you checked that the scripts are accessible?
    If you link to a test page that demonstrates the problem, we can find out what else is causing the problem.
    I hope to have it set up this week. Thanks.

    Quote Originally Posted by Max Height View Post
    You can do that without using some plug-in or jquery.
    Sure, have done it many times. But that wasn't the question now though, was it?

  6. #6
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by bbolte View Post
    But that wasn't the question now though, was it?
    Not sure.

    Lot's of noobies, and I'm not saying you're one, come to forums like this asking for help with jquery, plug-ins or whatever, thinking that jquery is some sort of shortcut to learning javascript (when it clearly isn't) and also not realising that what they want to do can very often be done much easier with vanilla javascript and always with much less code if they go look at the code jquery runs in the background to perform a given task.

  7. #7
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Max Height View Post
    Not sure...
    fair enough and valid points. thanks for the input.


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
  •