Answered by:
help with a javascript (simple) code

Question
-
i am kind of new to javascript coding, and have been struggling with some code I assume should be quite simple to write: I created a text field, and i want to restrict its values to numeric ones. I started with making a validation to raise an error if the value entered is not a numneric value. That's the script i wrote on the "On Change" event of the field:
// JScript source code var i = crmForm.all.new_test.DataValue // Check that the field is a number (no alphabetical values allowed) if (isNaN(i)) { alert('Please enter a number.'); crmForm.all.new_test.focus(); return false; }
If I enter an non-numeric value, the alert is raised, but I still can save the form. I tried to put the same validation on the onSave event of the form with no further success.
Any help would be appreciated !
Fadi
Wednesday, May 18, 2011 6:51 AM
Answers
-
Hello Fadi,
To restrict Save of form you should use following code at OnSave event handler:
var i = crmForm.all.new_test.DataValue; if (isNaN(i)) { alert('Please enter a number.'); event.returnValue = false; return false; }
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)- Marked as answer by Fadi Abou Serhal Wednesday, May 18, 2011 1:34 PM
Wednesday, May 18, 2011 7:24 AMModerator -
Hi Fadi,
Also if your textfield is a business required field you can just erase the data after alert message. So there would be no neccessity of on save event.
if (isNaN(i))
{
alert('Please enter a number.'); //also can alert the user entered value if you want.
crmForm.all.new_test.DataValue=null;}
Regards,Ravi
- Proposed as answer by Ravi Kumar T Wednesday, May 18, 2011 12:17 PM
- Marked as answer by Fadi Abou Serhal Wednesday, May 18, 2011 1:34 PM
Wednesday, May 18, 2011 12:16 PM
All replies
-
Hello Fadi,
To restrict Save of form you should use following code at OnSave event handler:
var i = crmForm.all.new_test.DataValue; if (isNaN(i)) { alert('Please enter a number.'); event.returnValue = false; return false; }
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)- Marked as answer by Fadi Abou Serhal Wednesday, May 18, 2011 1:34 PM
Wednesday, May 18, 2011 7:24 AMModerator -
Hi Fadi,
Also if your textfield is a business required field you can just erase the data after alert message. So there would be no neccessity of on save event.
if (isNaN(i))
{
alert('Please enter a number.'); //also can alert the user entered value if you want.
crmForm.all.new_test.DataValue=null;}
Regards,Ravi
- Proposed as answer by Ravi Kumar T Wednesday, May 18, 2011 12:17 PM
- Marked as answer by Fadi Abou Serhal Wednesday, May 18, 2011 1:34 PM
Wednesday, May 18, 2011 12:16 PM -
Thanks Andriy and Ravi for your help!
I used the combination of your suggestions:
if (isNaN(i)) { alert('Please enter a number'); crmForm.all.new_test.DataValue=null; event.returnValue = false; return false; }
and got the exact behavior i was looking for: the field is emptied and the focus remains on the field itself!
Wednesday, May 18, 2011 1:36 PM