By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Help : Validation for numeric type (PHP+JS)

P: 9
Hi..
i have field in record using type : numeric,

how to make validation and i want viewing messagebox if user inputing character or empty?

this code just work for empty field, not for numeric type,
if(WithoutContent(document.add.sprice.value))
{ errormessage += "\n\n\"PRICE\" still Empty."; }
----------

this my full code:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="JavaScript">
  2. <!-- 
  3. function CheckValidation() {
  4. var errormessage = new String();
  5.  
  6. if(WithoutContent(document.add.sprice.value))
  7.     { errormessage += "\n\n\"PRICE\" still Empty."; }
  8. if(WithoutContent(document.add.spropadd.value))
  9.     { errormessage += "\n\nPROPERTY ADD still Empty."; }
  10. if(WithoutSelectionValue(document.add.sbed))
  11.     { errormessage += "\n\nBED list still Empty."; }
  12.  
  13. if(errormessage.length > 2) {
  14.     alert('NOTE:' + errormessage);
  15.     return false;
  16.     }
  17. return true;
  18.  
  19. function WithoutContent(ss) {
  20. if(ss.length > 0) { return false; }
  21. return true;
  22. }
  23.  
  24. function WithoutSelectionValue(ss) {
  25. for(var i = 0; i < ss.length; i++) {
  26.     if(ss[i].selected) {
  27.         if(ss[i].value.length) { return false; }
  28.         }
  29.     }
  30. return true;
  31. }
  32. //-->    
  33. </script>
  34. .
  35. .
  36. .
  37. <form name="add" onsubmit="return CheckValidation()" action="inputing.php" method="POST" enctype="multipart/form-data">
  38.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

thanks b4..
Jun 8 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Moving to the JavaScript forum.
Jun 8 '07 #2

gits
Expert Mod 5K+
P: 5,282
hi ...

to test for a numeric value you may use javascripts isNaN() (isNotaNumber) method:

Expand|Select|Wrap|Line Numbers
  1. var foo = '12345.56';
  2. var bar = '12345gh';
  3.  
  4. // test will be false
  5. var test = isNaN(foo);
  6.  
  7. // test1 will be true
  8. var test1 = isNaN(bar);
  9.  
kind regards ...
Jun 9 '07 #3

dmjpro
100+
P: 2,476
Hi..
i have field in record using type : numeric,

how to make validation and i want viewing messagebox if user inputing character or empty?

this code just work for empty field, not for numeric type,
if(WithoutContent(document.add.sprice.value))
{ errormessage += "\n\n\"PRICE\" still Empty."; }
----------

this my full code:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="JavaScript">
  2. <!-- 
  3. function CheckValidation() {
  4. var errormessage = new String();
  5.  
  6. if(WithoutContent(document.add.sprice.value))
  7.     { errormessage += "\n\n\"PRICE\" still Empty."; }
  8. if(WithoutContent(document.add.spropadd.value))
  9.     { errormessage += "\n\nPROPERTY ADD still Empty."; }
  10. if(WithoutSelectionValue(document.add.sbed))
  11.     { errormessage += "\n\nBED list still Empty."; }
  12.  
  13. if(errormessage.length > 2) {
  14.     alert('NOTE:' + errormessage);
  15.     return false;
  16.     }
  17. return true;
  18.  
  19. function WithoutContent(ss) {
  20. if(ss.length > 0) { return false; }
  21. return true;
  22. }
  23.  
  24. function WithoutSelectionValue(ss) {
  25. for(var i = 0; i < ss.length; i++) {
  26.     if(ss[i].selected) {
  27.         if(ss[i].value.length) { return false; }
  28.         }
  29.     }
  30. return true;
  31. }
  32. //-->    
  33. </script>
  34. .
  35. .
  36. .
  37. <form name="add" onsubmit="return CheckValidation()" action="inputing.php" method="POST" enctype="multipart/form-data">
  38.  
[Please use CODE tags when posting source code. Thanks! --pbmods]

thanks b4..

U also can do this by stop processing the non-numeric character.

Expand|Select|Wrap|Line Numbers
  1. function validateNumeric()
  2. {
  3.  if(event.keyCode >=48 && event.keyCode <= 57);
  4.  else event.keyCode = 0; //Stop processing the non-numeric character.
  5. }
  6. //Mind it event.keyCode ..... this is IE specific and i hope u know for others.
  7.  
Try it.
Best of luck.

Kind regards,
Dmjpro.
Jun 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.