Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate an email id in xml schema

Hi I have created a schema to check for email id. which can validate if the email id is [email protected] and [email protected] and [email protected] But i want to validate only [email protected] and [email protected] 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

like image 491
Sunil Kumar Sahoo Avatar asked Jan 27 '10 14:01

Sunil Kumar Sahoo


People also ask

Can we validate XML schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported.

How can I check if an email address is valid regex?

[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times. @ matches itself.


1 Answers

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>
like image 150
Jeff Yates Avatar answered Sep 20 '22 13:09

Jeff Yates