To add form validation, you'll have to create the file to work on, and hook it to the Magento layout.
Add JavaScript to Sales Order Create
To follow best practices, you'll have to create your own module if you don't have already.
I'll start from bottom up, to evidentiate the core need:
Create a layout file
app/design/adminhtml/default/default/layout/mycustom/adminthml.xml
<?xml version="1.0"?>
<layout>
<adminhtml_sales_order_create_index>
<reference name="head">
<action method="addJs">
<script>mycustom/mycustom.js</script>
</action>
</reference>
</adminhtml_sales_order_create_index>
</layout>
You can skip the mycustom folder if you'd like and name your js file as you want - but remember to change accordingly also in the next steps.
Now you need to reference the xml layout file you just created.
Create your module
Create app\code\local\Mycustom\Adminhtml\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycustom_Adminhtml>
<version>0.0.1</version>
</Mycustom_Adminhtml>
</modules>
<adminhtml>
<layout>
<updates>
<mycustom>
<!-- this will look for the XML file in the app/design/adminhtml/default/default/layout folder -->
<file>mycustom/adminhtml.xml</file>
</mycustom>
</updates>
</layout>
</adminhtml>
</config>
Again, you can also skip the folder and name the xml file as you want.
Now you need to reference your module.
Create \app\etc\modules\Mycustom_Adminhtml.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycustom_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Adminhtml/>
<Mage_Sales />
</depends>
</Mycustom_Adminhtml>
</modules>
</config>
Now you can create your Javascript file \js\mycustom\mycustom.js
and put your validation code in it.
For JS form validations you can check this answers: Custom form Validation - Magento
What to be aware of/check:
- Look into Configuration > Advanced > Advanced to see if your new module is listed and Enabled
- Log files in /var/log folder
- Typing errors
- Root of mentioned paths is /web/ of your Magento installation folder
More detailed version here: Add JS to Magento Sales Order
This appears to be a core bug within Magento itself. The string for the regular expression isn't being converted properly using new RegExp()
or if you provide a native regular expression (e.g. /[a-z][a-z0-9]+/i
) it's not interpreted as such.
I submitted the issue via GitHub and confirmed that it exists in Magento CE 2.1.7 as well as the EE. Hopefully they fix the bug in the next release.
https://github.com/magento/magento2/issues/9919
EDIT: As of Magento 2.2.2 this has been fixed.
Best Answer
At the moment making telephone optional requires quite a lot of coding. There is a discussion at github about this resulting in a module (github:/zsoerenm/magento2-optionaltelephone) being developed thats sole purpose is exactly this. However, the magento devs have already implemented a solution in core to address this. The fix in the development branch now and hopefully will soon be in master.