SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic form validation

    Hi,
    I'm not good with Javascript, so please help me.

    I have a page generate by ASP and I want to validate the form value.
    Generated page by ASP :

    <form method=post action="<%=strPrgName%>" name="OrderForm">
    <% for i = 1 to itemcount %>
    Name : <input name=toname<%=i%> onBlur="validateName(this)">
    <% next %>
    <input type=submit value="View">

    For example itemcount = 2, html code will be like this :
    <form method=post action="<%=strPrgName%>" name="OrderForm">
    Name : <input name=toname1 onBlur="validateName(this)">
    Name : <input name=toname2 onBlur="validateName(this)">
    <input type=submit value="View">

    Javascript :
    <script language="JavaScript">
    function validateName(field)
    {
    var val = field.value;
    if(!/[a-z]/.test(val)||val=="")
    {
    alert("Format name wrong !");
    field.focus();
    field.select();
    }
    }
    </script>

    I got this script and modify it, the original code for validating number only. My need is this script validate the form to check if input only alphanumeric (a-z, A-Z, "," and ".").

    How to do it ? Thanks.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change this

    if(!/[a-z]/.test(val)||val=="")

    to this

    if(!/^[a-zA-Z,\.]+$/.test(val))

    Also, if you're looking for validation, I'd be please if you checked out my validation project, fValidate

    With fValidate, you could validate that field the same way with this:
    Code:
    <input type="text" name="Test" alt="alnum|*|A|0|0|,.">
    Note: fValidate is a onSubmit-based validation routine, not onBlur.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you're quick response. It worked now
    I have visited you're site before I post this, but I can't understand the code So, I post my problem to this forum.

    Another question,
    as you can see in my first post, i have a form generated by ASP. so
    <input name=namei> will vary, dependent on user choice.
    i've been thinking using onSubmit, but since I can't make sure the amount of namei> so i'm using onBlur. the problem is, if user didn't type anything or just click submit, onBlur event not working.
    What's your suggestion ? Is there anyway to check field name, and generate a looping for namei> it automatically ?
    Last edited by jet_wang; Oct 8, 2002 at 18:47.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just have your ASP add the validation codes for fValidate, instead of the onBlur event?

    <% for i = 1 to itemcount %>
    Name : <input name=toname<%=i%> alt="alnum|*|A|0|0|,.">
    <% next %>
    <input type=submit value="View">


    ???
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you more specific how to do it ?
    do i just put the alt="alnum|*|A|0|0|,." only ? then how to do the validation ?

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not exactly....

    Read this first, let me know if you are still stuck after that...
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi beetle, i'm sorry to bothering you again.
    i tried your script, the code like this :

    <html>

    <script src="scripts/fValConfig.js"></script>
    <script src="scripts/fValidateDemo.js"></script>

    <body>

    <form name="test" method="post" action="" onSubmit="return validateForm(this)" >
    <input name="Name" alt="blank" onBlur="clearStyle(this);" />
    <input type="submit" name="Submit" value="Check it" />
    </form>

    </body>
    </html>

    but it seem not working. what's the problem ? thanks

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you download the zip file? The filename for fValidate.js is incorrect. Remove the red text from below

    <script src="scripts/fValidateDemo.js"></script
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wouw...you really know my problem. it's working now
    you're script really cool.
    one more thing, it seem the clearstyle not working, i mean the input box's color don't change after error like in your site.
    Last edited by jet_wang; Oct 23, 2002 at 20:00.

  10. #10
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got a link?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  11. #11
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Jakarta
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no i forgot my domaindlx password. i'll try to request password again. please be patient with me


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
  •