Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question validate fields for numeric values only

    Hello guys,

    How can I validate that users have entered text or anything else for that matter and give them a pop up message informing they can only enter numbers.

    I am new to this as you can see by my question,I hope somebody can help.

    Regards

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Here is an example:-

    <tr>
    <td><font face="Arial"><em><b>Club Membership No. (if applicable)</b></em></font></td>
    <td><input type="text" size="8" maxlength="8" name="MemNumber" onblur="if(/\D/g.test(this.value)){alert('Only current Club membership numbers are valid in this box. ');this.value='';this.focus()}; </font></td></tr>

  • #3
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a script that works the same as Philip's.

    Code:
    var f = document.forms[0]
    
    var v = f.NumField.value
    for (i=0; i<v.length; i++)
    {
    	if ((v.charCodeAt(i)<48 || v.charCodeAt(i)>57) && v.charCodeAt(i) !== 44 ) 
    	{
    	alert ( "This must be numeric." )
    	f.NumField.value = "";
    	f.NumField.focus()
    	return false
    	}
    }

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if(parseFloat(Field.value,10)==Field.value){}
    or
    if(parseInt(Field.value,10)==Field.value){}

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    parseFloat doesn't need that second argument of 10 since it may only take decimal numbers, but the parseInt needs it.
    Last edited by liorean; 04-26-2004 at 10:43 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,964
    Thanks
    0
    Thanked 236 Times in 233 Posts
    parseFloat() has no radix parameter unlike parseInt(). It always parse numbers in base 10

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by glenngv
    parseFloat() has no radix parameter unlike parseInt(). It always parse numbers in base 10
    Beat you to it
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,964
    Thanks
    0
    Thanked 236 Times in 233 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •