Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read Web Directory

Go Back   Webmaster Forum > Designing and Developing Websites > Programming > Ajax / Javascript

Ajax / Javascript Discussions and help on Ajax and Javascript coding.

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-13-2007, 05:08 AM
technoguy technoguy is offline
WMG Citizen
 
Join Date: Mar 2007
Posts: 436
iTrader: (0)
technoguy is on a distinguished road
Default Validation for numeric data

Is there anyone who can provide me code for validation for numbers only field?

Thank you
Reply With Quote
Sponsored Links
Register and sign in to hide this ad block

  #2 (permalink)  
Old 03-13-2007, 04:35 PM
Tripy Tripy is offline
WMG Newcomer
 
Join Date: Mar 2007
Posts: 31
iTrader: (0)
Tripy is on a distinguished road
Default

What kind of validation. For it to be numeric only ?
Positive numeric only, or can it be negative too (like -20)

If it's strictly numeric, then isNan() is your friend.
Code:
var value=10;
var result=null;
//isNan() test if the parameter is NOT a number. So testing that a number is NOT not a number does the trick.
if(!isNan(value)){
  result=true;
}
else{
  result=false;
}
reference: http://docs.sun.com/source/816-6408-...ev.htm#1064024
Reply With Quote
  #3 (permalink)  
Old 03-13-2007, 04:46 PM
technoguy technoguy is offline
WMG Citizen
 
Join Date: Mar 2007
Posts: 436
iTrader: (0)
technoguy is on a distinguished road
Default

I am new to javascript. So plz advice , should I call javascript function on Submit button click event? I mean while submitting a forum?
Reply With Quote
  #4 (permalink)  
Old 03-13-2007, 08:00 PM
Tripy Tripy is offline
WMG Newcomer
 
Join Date: Mar 2007
Posts: 31
iTrader: (0)
Tripy is on a distinguished road
Default

It's up to each one preferences.
There is not just 1 way of doing something.

I personally prefer to use a simple button, that will call a javascript fnuction that will do the submit, rather than a submit button that I will stop the effect later.
Simply the logic seems wrong to stop an action that have started.
I prefer to check it before.

Look at this bit of javascript:
Code:
<script type="text/javascript">
function submitForm(){
  var inputError=true;
  //For a text input
  var val1=document.getElementById('field1').value;
  //For a drop down select
  var sel1=document.getElementById('select1').options[document.getElementById('select1').selectedIndex].value
  //For a radio button
  for(i=0;i<document.getElementById('radio1').length;i++){
    if(document.getElementById('radio1').checked){
      var radio1=document.getElementById('radio1').value;
    }
  }
  
  //Now, do the checks you need with your values...
  ...
  if(1 == 1){
    //Just for the demo, we validate the datas
    inputError=false;
  }
  //And if there are no errors, simply submit the form
  if(inputError==false){
    document.getElementById('myForm').submit();
  }
}
</script>
This function goes with this form:

Code:
<form name="myForm" id="myForm" method="post" action="http://www.whatever.com"/>
  <input type="text" name="field1" id="field1"/><br/>
  <select name="select1" id="select1">
    <option value="1">First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
  </select><br/>
  <input type="radio" name="radio1" id="radio1" value="one"/><br/>
  <input type="radio" name="radio1" id="radio1" value="two"/><br/>
  <input type="radio" name="radio1" id="radio1" value="three"/><br/>
  <input type="button" value="click me, you fool" onclick="javascript:submitForm();"/>
</form>
And the combination of those two will make the form validated and submitted upon the click on the button, via the calling of the javascript function.
Reply With Quote
  #5 (permalink)  
Old 03-14-2007, 04:44 AM
technoguy technoguy is offline
WMG Citizen
 
Join Date: Mar 2007
Posts: 436
iTrader: (0)
technoguy is on a distinguished road
Default

Ok I have managed to validate for numeric data now. What I want to do now is when page is fully loaded cursor should go to first textbox automatically. I mean i want to set the focus on the text box. So that I dont have to click everytime inside the text box for typing there.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT. The time now is 06:05 PM.



Freelance Web Designers
Work At Home Forum
Ad Marketplace
Online Deals and Bargains
Lunarpages Web Hosting Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0