XML Scheme validation for different types

Viewed 64

Im trying to make an xml validiation code for e-mails which should be end with

  • @gmail.com
  • @hotmail.com
  • @yahoo.com

I have implemented a code for gmail but I didn't figure out how can I create a pattern for 3 different type.

<xs:element name="email">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Za-z_.0-9]+@gmail\.com"></xs:pattern>
        </xs:restriction>
     </xs:simpleType>
</xs:element>
1 Answers

You can use

<xs:pattern value="[A-Za-z0-9_.]+@(gmail|hotmail|yahoo)\.com"></xs:pattern>
<xs:pattern value="[^@]+@(gmail|hotmail|yahoo)\.com"></xs:pattern>

The (gmail|hotmail|yahoo)\.com part will match gmail.com, hotmail.com or yahoo.com. Note you can only use capturing groups in XSD regex, so no (?:...) allowed.

Note [^@]+ matches any one or more chars other than @ and is a bit more loose email validation pattern, just in case you need to allow more chars in the user name.

Related