SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    advace validation with js

    hello friends.

    i am php programmer, i want to do js validation with my form.
    in details like

    !)i have product id, and that wanted to be an alphanumeric.
    that is working perfectly fine with js.

    2)but i want some formated alphanumeric value
    e.g.
    prod_id=L28
    means first character should be alphabet and after i want only two digit

    may be its too simple but i am new comer.
    so please help me.
    thanks in advance

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easiest way to validate this is to use regular expressions. Get the value of the field and use the regular expression to validate.

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could u please explain me with example means how i can do tht?

  4. #4
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i ll tell you, what i am doing exactly,
    PHP Code:
    if(document.productFrm.prod_id.value=="")
        {
            
    alert("Please enter your product id");
            
    document.productFrm.prod_id.focus();
            return 
    false;
        } 
    and i am chacking my alphanumeric value through php
    PHP Code:
    }else if (!ctype_alnum($prod_id)){
        
    //  echo " This is alphanumeric <br/>";


                  
    $prod_noErr      "Please enter alphanumeric value";
                  
    $validate           0
    so please tell me, how to do this with js?

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out this thread.

    You need to use the same concept. Your regular expressions to validate a word character followed by two numbers would look like this:
    Code:
    var re = /^\w\d{2}$/;
    You can then check that the entered value matches your requirements in the following way:
    Code:
    var value = document.productFrm.prod_id.value;
    var re = /^\w\d{2}$/;
    
    if (re.test(value)) {
        alert("Value is valid");
        return true;
    } else {
        alert("Entered value is not correct");
        return false;
    }

  6. #6
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot pepejeria,

    i will have look first on your links, and then ll try to understand how it works, and then let you know ok.

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP also provides good Regular Expression support. You might also want to validate the value on the server as well.

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i am working on that also
    see
    what i have done here
    PHP Code:
    function validate() {


               var 
    value document.productFrm.prod_id.value;
                 var 
    re =  /^\w\d{2}$/;
        
        if(
    document.productFrm.prod_id.value=="")
        {
            
    alert("Please enter your product id");
            
    document.productFrm.prod_id.focus();
            return 
    false;
        }
        else if((
    document.productFrm.prod_id.value !="") && (isNaN(document.prod_id.value)==false))
        {
          if (
    re.test(value)) {
              
    alert("Value is valid");
              return 
    true;
          } else {
              
    alert("Entered value is not correct");
              return 
    false;
    }
    }

    but not working.

    query-
    1)re.test(value)- what is this? (is this function)

  9. #9
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code has several serious errors. Check out the Error Console in Firefox.

    For example, missing } at the end and isNaN is misspelled. Also, why did you remove the backslashes from the regular expression?

  10. #10
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i have removed all errors
    and pattern is
    var re = /^\w\d{2}$/;

    ok, but its not working. please tell what is going wrong here

  11. #11
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your if else logic is a bit odd:
    Code:
     
        if (document.productFrm.prod_id.value=="") {
    
        } else if(document.productFrm.prod_id.value !="")
    There is no need to check if the string is not empty in the else since you already checked that in the if.

    Anyway, I assume you read the other thread?

    The ^ and the $ in the regular expression makes it unnecessary to check if the value empty is empty. Since it checks that the value starts with a word and ends with 2 numbers.


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
  •