14

Hi I have created a schema to check for email id. which can validate if the email id is abc@def.com and adbc@def.co.in and abc@def.co.in.pune But i want to validate only abc@def.com and adbc@def.co.in because i think email can have maximum 2 dots after @ symbol so the third one will be invalid email id So how to validate an email id using schema Below is the schema

<xsd:element name="SSEM" minOccurs="0">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="CNT" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="EM" minOccurs="1" nillable="true" type ="singleEmailID"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

Thanks Sunil Kumar Sahoo

Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 1
    `because i think email can have maximum 2 dots after @ symbol`. Maybe read relevant RFCs instead of guessing and arbitrarily failing validation? `abc@10.0.0.0` is a perfectly valid e-mail too. http://davidcel.is/posts/stop-validating-email-addresses-with-regex/ – Vsevolod Golovanov Sep 01 '15 at 13:38

4 Answers4

33

You will need to define a pattern to match against valid e-mails. Patterns are defined using regular expression syntax. Once you have defined a simple type (based on xs:string) with the appropriate pattern, you can use that for your e-mail type.

There are several places on the Internet that provide some examples of such types and patterns. An example of an e-mail type is provided here.

The example given there is as follows (I've edited it slightly to make things a little clearer):

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 

  <xsd:element name="A" type="emailAddress"/> 

  <xsd:simpleType name="emailAddress"> 
    <xsd:restriction base="xsd:string"> 
      <xsd:pattern value="[^@]+@[^\.]+\..+"/> 
    </xsd:restriction> 
  </xsd:simpleType> 
</xsd:schema>
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
7

You could use a <xs:simpleType> based on a string and with a regex pattern to validate your e-mail addresses:

<xsd:simpleType name="emailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})"/>
    </xsd:restriction>
</xsd:simpleType>

Use any e-mail regex you like :-), see some samples on RegexLib.Net.

Then, use that type in your basic XML schema:

<xsd:element name="email" type="emailAddress" />

Could by checked by online validator: https://www.corefiling.com/opensource/schemaValidate.html

yegor256
  • 102,010
  • 123
  • 446
  • 597
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 6
    Unlike other RegEx libraries, XML Schema patterns do not start with ^ and end with a $. This is implicit in atomic strings. – Dan McCreary Jan 28 '14 at 18:52
1

Use the below schema validator for email id validation

<xsd:simpleType name="emailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>
    </xsd:restriction>
</xsd:simpleType>
Pacerier
  • 86,231
  • 106
  • 366
  • 634
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
0

Use this validator for email validation with apostrophe also:

<xsd:simpleType name="emailAddress">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="^([0-9a-zA-Z_\.\'\-]+)*@[0-9a-zA-Z\-]+[a-zA-Z\.]+Dollar symbol"/>
    </xsd:restriction>
</xsd:simpleType>

It will work as email validation with apostrophe:-)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shans Muga
  • 75
  • 2
  • 3
  • 9